Przeglądaj źródła

授权添加系统字段

785834757 1 rok temu
rodzic
commit
6c8aeff5bb

+ 4 - 2
authentication/src/main/java/com/jzg/config/JzgAuthenticationProvider.java

@@ -3,6 +3,7 @@ package com.jzg.config;
 import com.jzg.entity.RedisToken;
 import com.jzg.entity.SysUserRole;
 import com.jzg.entity.SysUser;
+import com.jzg.exception.SystemException;
 import com.jzg.service.SysUserRoleService;
 import com.jzg.service.SysUserService;
 import com.jzg.util.PasswordUtils;
@@ -38,6 +39,7 @@ public class JzgAuthenticationProvider implements AuthenticationProvider {
         String username = authentication.getName();
         String rawPassword = (String) authentication.getCredentials();
         Object details = authentication.getDetails();
+        String system = ((JzgUsernamePasswordAuthenticationToken)authentication).getSystem();
 
         SysUser byId = sysUserService.getById(username);
         String salt = byId.getSalt();
@@ -47,13 +49,13 @@ public class JzgAuthenticationProvider implements AuthenticationProvider {
 
         // 对比密码
         if (!encryptedPassword.equals(byId.getPassword())) {
-            throw new BadCredentialsException("Invalid username or password");
+            throw new SystemException("用户名或密码错误");
         }
         //获取角色组
         List<SysUserRole> userRoles = sysUserRoleService.getUserRole(username);
 
         List<JzgGrantedAuthority> authorities = userRoles.stream()
-                .map(role -> new JzgGrantedAuthority("JZG",role.getRoleId()))
+                .map(role -> new JzgGrantedAuthority(system,role.getRoleId()))
                 .collect(Collectors.toList());
 
         return new UsernamePasswordAuthenticationToken(username, rawPassword, authorities);

+ 23 - 0
authentication/src/main/java/com/jzg/config/JzgUsernamePasswordAuthenticationToken.java

@@ -0,0 +1,23 @@
+package com.jzg.config;
+
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+
+public class JzgUsernamePasswordAuthenticationToken extends UsernamePasswordAuthenticationToken {
+
+    /**
+     * 系统标识
+     */
+    private String system;
+    public JzgUsernamePasswordAuthenticationToken(Object principal, Object credentials,String system) {
+        super(principal, credentials);
+        this.system = system;
+    }
+
+    public void setSystem(String s){
+        this.system = s;
+    }
+
+    public String getSystem(){
+        return this.system;
+    }
+}

+ 3 - 2
authentication/src/main/java/com/jzg/controller/AuthController.java

@@ -4,6 +4,7 @@ package com.jzg.controller;
 import com.jzg.aop.PremissionCheck;
 import com.jzg.config.JzgTokenService;
 import com.jzg.config.JzgUserDetailsService;
+import com.jzg.config.JzgUsernamePasswordAuthenticationToken;
 import com.jzg.entity.SysUser;
 import com.jzg.handler.JzgAuthenticationSuccessHandler;
 import com.jzg.service.SysUserService;
@@ -47,10 +48,10 @@ public class AuthController {
      * @throws IOException
      */
     @GetMapping("/login")
-    public void login(String userName,String passWord) throws ServletException, IOException {
+    public void login(String userName,String passWord,String system) throws ServletException, IOException {
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
-        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userName,passWord);
+        JzgUsernamePasswordAuthenticationToken authentication = new JzgUsernamePasswordAuthenticationToken(userName,passWord,system);
         Authentication authenticate = authenticationManager.authenticate(authentication);
         SecurityContextHolder.getContext().setAuthentication(authenticate);
         JzgAuthenticationSuccessHandler successHandler = new JzgAuthenticationSuccessHandler(tokenStore);