Browse Source

优化权限获取逻辑

zhouhao 8 years ago
parent
commit
5040f1b2a0

+ 30 - 26
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/ListenerAuthorizingRealm.java

@@ -29,11 +29,13 @@ import org.apache.shiro.cache.Cache;
 import org.apache.shiro.realm.AuthorizingRealm;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.apache.shiro.subject.Subject;
-import org.hswebframework.web.authorization.Authorization;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationManager;
 import org.hswebframework.web.authorization.Role;
 import org.hswebframework.web.authorization.listener.AuthorizationListener;
 import org.hswebframework.web.authorization.listener.event.AuthorizationSuccessEvent;
 
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -41,36 +43,38 @@ import java.util.stream.Collectors;
  */
 public class ListenerAuthorizingRealm extends AuthorizingRealm
         implements AuthorizationListener<AuthorizationSuccessEvent> {
+    private AuthenticationManager authenticationManager;
 
-    public ListenerAuthorizingRealm() {
+
+    public ListenerAuthorizingRealm(AuthenticationManager authenticationManager) {
+        Objects.requireNonNull(authenticationManager);
+        this.authenticationManager=authenticationManager;
         setAuthenticationTokenClass(SimpleAuthenticationToken.class);
     }
 
+
     @Override
     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
-        String loginName = (String) super.getAvailablePrincipal(principals);
-        return this.<String, AuthorizationInfo>getCache(loginName)
-                .get(AuthorizationInfo.class.getName());
+        String loginUserId = (String) super.getAvailablePrincipal(principals);
+        return createAuthorizationInfo(authenticationManager.getByUserId(loginUserId));
     }
 
     @Override
     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
         if (token instanceof SimpleAuthenticationToken) {
-            return this.<String, AuthenticationInfo>getCache((String) token.getPrincipal())
-                    .get(AuthenticationInfo.class.getName());
+            return createAuthenticationInfo(((SimpleAuthenticationToken) token).getAuthentication());
         }
         throw new AuthenticationException(new UnsupportedOperationException("{token_un_supported}"));
     }
 
-    private AuthenticationInfo createAuthenticationInfo(Authorization authorization) {
+    private AuthenticationInfo createAuthenticationInfo(Authentication authentication) {
         return new SimpleAuthenticationInfo(
-                authorization.getUser().getUsername(),
-                authorization.getUser().getId(), ListenerAuthorizingRealm.class.getName());
+                authentication.getUser().getId(),
+                authentication.getUser().getUsername(),
+                ListenerAuthorizingRealm.class.getName());
     }
 
-    public void loginOut(Authorization authorization) {
-        if (null != authorization)
-            getCache(authorization.getUser().getUsername()).clear();
+    public void loginOut(Authentication authentication) {
         SecurityUtils.getSubject().logout();
     }
 
@@ -82,15 +86,11 @@ public class ListenerAuthorizingRealm extends AuthorizingRealm
         return "shiro.auth.info.".concat(name);
     }
 
-    @Override
-    public void on(AuthorizationSuccessEvent event) {
-        Authorization authorization = event.getAuthorization();
-        boolean remember = Boolean.TRUE.equals(event.getParameter("remember").orElse(false));
-
+    protected AuthorizationInfo createAuthorizationInfo(Authentication authentication) {
         SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
-        authorizationInfo.addRoles(authorization.getRoles().stream().map(Role::getId).collect(Collectors.toList()));
+        authorizationInfo.addRoles(authentication.getRoles().stream().map(Role::getId).collect(Collectors.toList()));
         authorizationInfo.addObjectPermissions(
-                authorization.getPermissions()
+                authentication.getPermissions()
                         .stream()
                         .map(permission -> {
                             StringBuilder builder = new StringBuilder(permission.getId());
@@ -100,14 +100,18 @@ public class ListenerAuthorizingRealm extends AuthorizingRealm
                             return new WildcardPermission(builder.toString());
                         }).collect(Collectors.toList()));
 
-        getCache(authorization.getUser().getUsername())
-                .put(AuthorizationInfo.class.getName(), authorizationInfo);
+        return authorizationInfo;
+    }
+
+    @Override
+    public void on(AuthorizationSuccessEvent event) {
+        Authentication authentication = event.getAuthentication();
+        boolean remember = Boolean.valueOf((String) event.getParameter("remember").orElse("false"));
 
-        getCache(authorization.getUser().getUsername())
-                .put(AuthenticationInfo.class.getName(), createAuthenticationInfo(authorization));
+//        authentication.setAttribute(AuthorizationInfo.class.getName(), authorizationInfo);
+//        authentication.setAttribute(AuthenticationInfo.class.getName(), createAuthenticationInfo(authentication));
 
         Subject subject = SecurityUtils.getSubject();
-        subject.login(new SimpleAuthenticationToken(authorization, remember));
-        subject.getSession().setAttribute(Authorization.class.getName(), authorization);
+        subject.login(new SimpleAuthenticationToken(authentication, remember));
     }
 }

+ 8 - 8
hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/SimpleAuthenticationToken.java

@@ -20,7 +20,7 @@ package org.hswebframework.web.authorization.shiro;
 import org.apache.shiro.authc.AuthenticationToken;
 import org.apache.shiro.authc.HostAuthenticationToken;
 import org.apache.shiro.authc.RememberMeAuthenticationToken;
-import org.hswebframework.web.authorization.Authorization;
+import org.hswebframework.web.authorization.Authentication;
 
 /**
  * TODO 完成注释
@@ -28,29 +28,29 @@ import org.hswebframework.web.authorization.Authorization;
  * @author zhouhao
  */
 public class SimpleAuthenticationToken implements AuthenticationToken, HostAuthenticationToken, RememberMeAuthenticationToken {
-    private Authorization authorization;
+    private Authentication authentication;
 
     private boolean rememberMe;
 
     private String host;
 
-    public SimpleAuthenticationToken(Authorization authorization, boolean rememberMe) {
-        this.authorization = authorization;
+    public SimpleAuthenticationToken(Authentication authentication, boolean rememberMe) {
+        this.authentication = authentication;
         this.rememberMe = rememberMe;
     }
 
     @Override
     public Object getPrincipal() {
-        return authorization.getUser().getUsername();
+        return authentication.getUser().getId();
     }
 
     @Override
     public Object getCredentials() {
-        return authorization.getUser().getId();
+        return authentication.getUser().getUsername();
     }
 
-    public Authorization getAuthorization() {
-        return authorization;
+    public Authentication getAuthentication() {
+        return authentication;
     }
 
     @Override