chenshudong пре 3 недеља
родитељ
комит
c7fea2d447

+ 10 - 19
airport-common/src/main/java/com/sundot/airport/common/core/domain/model/LoginBody.java

@@ -2,11 +2,10 @@ package com.sundot.airport.common.core.domain.model;
2 2
 
3 3
 /**
4 4
  * 用户登录对象
5
- * 
5
+ *
6 6
  * @author ruoyi
7 7
  */
8
-public class LoginBody
9
-{
8
+public class LoginBody {
10 9
     /**
11 10
      * 用户名
12 11
      */
@@ -27,43 +26,35 @@ public class LoginBody
27 26
      */
28 27
     private String uuid;
29 28
 
30
-    public String getUsername()
31
-    {
29
+    public String getUsername() {
32 30
         return username;
33 31
     }
34 32
 
35
-    public void setUsername(String username)
36
-    {
33
+    public void setUsername(String username) {
37 34
         this.username = username;
38 35
     }
39 36
 
40
-    public String getPassword()
41
-    {
37
+    public String getPassword() {
42 38
         return password;
43 39
     }
44 40
 
45
-    public void setPassword(String password)
46
-    {
41
+    public void setPassword(String password) {
47 42
         this.password = password;
48 43
     }
49 44
 
50
-    public String getCode()
51
-    {
45
+    public String getCode() {
52 46
         return code;
53 47
     }
54 48
 
55
-    public void setCode(String code)
56
-    {
49
+    public void setCode(String code) {
57 50
         this.code = code;
58 51
     }
59 52
 
60
-    public String getUuid()
61
-    {
53
+    public String getUuid() {
62 54
         return uuid;
63 55
     }
64 56
 
65
-    public void setUuid(String uuid)
66
-    {
57
+    public void setUuid(String uuid) {
67 58
         this.uuid = uuid;
68 59
     }
69 60
 }

+ 36 - 40
airport-framework/src/main/java/com/sundot/airport/framework/config/SecurityConfig.java

@@ -23,19 +23,18 @@ import com.sundot.airport.framework.security.handle.LogoutSuccessHandlerImpl;
23 23
 
24 24
 /**
25 25
  * spring security配置
26
- * 
26
+ *
27 27
  * @author ruoyi
28 28
  */
29 29
 @EnableMethodSecurity(prePostEnabled = true, securedEnabled = true)
30 30
 @Configuration
31
-public class SecurityConfig
32
-{
31
+public class SecurityConfig {
33 32
     /**
34 33
      * 自定义用户认证逻辑
35 34
      */
36 35
     @Autowired
37 36
     private UserDetailsService userDetailsService;
38
-    
37
+
39 38
     /**
40 39
      * 认证失败处理类
41 40
      */
@@ -53,7 +52,7 @@ public class SecurityConfig
53 52
      */
54 53
     @Autowired
55 54
     private JwtAuthenticationTokenFilter authenticationTokenFilter;
56
-    
55
+
57 56
     /**
58 57
      * 跨域过滤器
59 58
      */
@@ -70,8 +69,7 @@ public class SecurityConfig
70 69
      * 身份验证实现
71 70
      */
72 71
     @Bean
73
-    public AuthenticationManager authenticationManager()
74
-    {
72
+    public AuthenticationManager authenticationManager() {
75 73
         DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
76 74
         daoAuthenticationProvider.setUserDetailsService(userDetailsService);
77 75
         daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
@@ -94,46 +92,44 @@ public class SecurityConfig
94 92
      * authenticated       |   用户登录后可访问
95 93
      */
96 94
     @Bean
97
-    protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
98
-    {
95
+    protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
99 96
         return httpSecurity
100
-            // CSRF禁用,因为不使用session
101
-            .csrf(csrf -> csrf.disable())
102
-            // 禁用HTTP响应标头
103
-            .headers((headersCustomizer) -> {
104
-                headersCustomizer.cacheControl(cache -> cache.disable()).frameOptions(options -> options.sameOrigin());
105
-            })
106
-            // 认证失败处理类
107
-            .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
108
-            // 基于token,所以不需要session
109
-            .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
110
-            // 注解标记允许匿名访问的url
111
-            .authorizeHttpRequests((requests) -> {
112
-                permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
113
-                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
114
-                requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
115
-                    // 静态资源,可匿名访问
116
-                    .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
117
-                    .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
118
-                    // 除上面外的所有请求全部需要鉴权认证
119
-                    .anyRequest().authenticated();
120
-            })
121
-            // 添加Logout filter
122
-            .logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
123
-            // 添加JWT filter
124
-            .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
125
-            // 添加CORS filter
126
-            .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
127
-            .addFilterBefore(corsFilter, LogoutFilter.class)
128
-            .build();
97
+                // CSRF禁用,因为不使用session
98
+                .csrf(csrf -> csrf.disable())
99
+                // 禁用HTTP响应标头
100
+                .headers((headersCustomizer) -> {
101
+                    headersCustomizer.cacheControl(cache -> cache.disable()).frameOptions(options -> options.sameOrigin());
102
+                })
103
+                // 认证失败处理类
104
+                .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
105
+                // 基于token,所以不需要session
106
+                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
107
+                // 注解标记允许匿名访问的url
108
+                .authorizeHttpRequests((requests) -> {
109
+                    permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
110
+                    // 对于登录login 注册register 验证码captchaImage 允许匿名访问
111
+                    requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
112
+                            // 静态资源,可匿名访问
113
+                            .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
114
+                            .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
115
+                            // 除上面外的所有请求全部需要鉴权认证
116
+                            .anyRequest().authenticated();
117
+                })
118
+                // 添加Logout filter
119
+                .logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
120
+                // 添加JWT filter
121
+                .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
122
+                // 添加CORS filter
123
+                .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
124
+                .addFilterBefore(corsFilter, LogoutFilter.class)
125
+                .build();
129 126
     }
130 127
 
131 128
     /**
132 129
      * 强散列哈希加密实现
133 130
      */
134 131
     @Bean
135
-    public BCryptPasswordEncoder bCryptPasswordEncoder()
136
-    {
132
+    public BCryptPasswordEncoder bCryptPasswordEncoder() {
137 133
         return new BCryptPasswordEncoder();
138 134
     }
139 135
 }

+ 27 - 45
airport-framework/src/main/java/com/sundot/airport/framework/web/service/SysLoginService.java

@@ -1,6 +1,7 @@
1 1
 package com.sundot.airport.framework.web.service;
2 2
 
3 3
 import javax.annotation.Resource;
4
+
4 5
 import org.springframework.beans.factory.annotation.Autowired;
5 6
 import org.springframework.security.authentication.AuthenticationManager;
6 7
 import org.springframework.security.authentication.BadCredentialsException;
@@ -31,12 +32,11 @@ import com.sundot.airport.system.service.ISysUserService;
31 32
 
32 33
 /**
33 34
  * 登录校验方法
34
- * 
35
+ *
35 36
  * @author ruoyi
36 37
  */
37 38
 @Component
38
-public class SysLoginService
39
-{
39
+public class SysLoginService {
40 40
     @Autowired
41 41
     private TokenService tokenService;
42 42
 
@@ -45,7 +45,7 @@ public class SysLoginService
45 45
 
46 46
     @Autowired
47 47
     private RedisCache redisCache;
48
-    
48
+
49 49
     @Autowired
50 50
     private ISysUserService userService;
51 51
 
@@ -54,43 +54,34 @@ public class SysLoginService
54 54
 
55 55
     /**
56 56
      * 登录验证
57
-     * 
57
+     *
58 58
      * @param username 用户名
59 59
      * @param password 密码
60
-     * @param code 验证码
61
-     * @param uuid 唯一标识
60
+     * @param code     验证码
61
+     * @param uuid     唯一标识
62 62
      * @return 结果
63 63
      */
64
-    public String login(String username, String password, String code, String uuid)
65
-    {
64
+    public String login(String username, String password, String code, String uuid) {
66 65
         // 验证码校验
67 66
         validateCaptcha(username, code, uuid);
68 67
         // 登录前置校验
69 68
         loginPreCheck(username, password);
70 69
         // 用户验证
71 70
         Authentication authentication = null;
72
-        try
73
-        {
71
+        try {
74 72
             UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
75 73
             AuthenticationContextHolder.setContext(authenticationToken);
76 74
             // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
77 75
             authentication = authenticationManager.authenticate(authenticationToken);
78
-        }
79
-        catch (Exception e)
80
-        {
81
-            if (e instanceof BadCredentialsException)
82
-            {
76
+        } catch (Exception e) {
77
+            if (e instanceof BadCredentialsException) {
83 78
                 AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
84 79
                 throw new UserPasswordNotMatchException();
85
-            }
86
-            else
87
-            {
80
+            } else {
88 81
                 AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
89 82
                 throw new ServiceException(e.getMessage());
90 83
             }
91
-        }
92
-        finally
93
-        {
84
+        } finally {
94 85
             AuthenticationContextHolder.clearContext();
95 86
         }
96 87
         AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
@@ -102,27 +93,23 @@ public class SysLoginService
102 93
 
103 94
     /**
104 95
      * 校验验证码
105
-     * 
96
+     *
106 97
      * @param username 用户名
107
-     * @param code 验证码
108
-     * @param uuid 唯一标识
98
+     * @param code     验证码
99
+     * @param uuid     唯一标识
109 100
      * @return 结果
110 101
      */
111
-    public void validateCaptcha(String username, String code, String uuid)
112
-    {
102
+    public void validateCaptcha(String username, String code, String uuid) {
113 103
         boolean captchaEnabled = configService.selectCaptchaEnabled();
114
-        if (captchaEnabled)
115
-        {
104
+        if (captchaEnabled) {
116 105
             String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
117 106
             String captcha = redisCache.getCacheObject(verifyKey);
118
-            if (captcha == null)
119
-            {
107
+            if (captcha == null) {
120 108
                 AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
121 109
                 throw new CaptchaExpireException();
122 110
             }
123 111
             redisCache.deleteObject(verifyKey);
124
-            if (!code.equalsIgnoreCase(captcha))
125
-            {
112
+            if (!code.equalsIgnoreCase(captcha)) {
126 113
                 AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
127 114
                 throw new CaptchaException();
128 115
             }
@@ -131,35 +118,31 @@ public class SysLoginService
131 118
 
132 119
     /**
133 120
      * 登录前置校验
121
+     *
134 122
      * @param username 用户名
135 123
      * @param password 用户密码
136 124
      */
137
-    public void loginPreCheck(String username, String password)
138
-    {
125
+    public void loginPreCheck(String username, String password) {
139 126
         // 用户名或密码为空 错误
140
-        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
141
-        {
127
+        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
142 128
             AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
143 129
             throw new UserNotExistsException();
144 130
         }
145 131
         // 密码如果不在指定范围内 错误
146 132
         if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
147
-                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
148
-        {
133
+                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
149 134
             AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
150 135
             throw new UserPasswordNotMatchException();
151 136
         }
152 137
         // 用户名不在指定范围内 错误
153 138
         if (username.length() < UserConstants.USERNAME_MIN_LENGTH
154
-                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
155
-        {
139
+                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
156 140
             AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
157 141
             throw new UserPasswordNotMatchException();
158 142
         }
159 143
         // IP黑名单校验
160 144
         String blackStr = configService.selectConfigByKey("sys.login.blackIPList");
161
-        if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
162
-        {
145
+        if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
163 146
             AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked")));
164 147
             throw new BlackListException();
165 148
         }
@@ -170,8 +153,7 @@ public class SysLoginService
170 153
      *
171 154
      * @param userId 用户ID
172 155
      */
173
-    public void recordLoginInfo(Long userId)
174
-    {
156
+    public void recordLoginInfo(Long userId) {
175 157
         SysUser sysUser = new SysUser();
176 158
         sysUser.setUserId(userId);
177 159
         sysUser.setLoginIp(IpUtils.getIpAddr());

+ 2 - 1
airport-system/src/main/resources/mapper/system/SysUserMapper.xml

@@ -220,7 +220,8 @@
220 220
         </if>
221 221
         <!-- 角色名称模糊查询 -->
222 222
         <if test="roleName != null and roleName != ''">
223
-            AND u.user_id IN (SELECT user_id FROM sys_user_role sur INNER JOIN sys_role sr ON sur.role_id = sr.role_id WHERE sr.role_name like concat('%', #{roleName}, '%'))
223
+            AND u.user_id IN (SELECT user_id FROM sys_user_role sur INNER JOIN sys_role sr ON sur.role_id = sr.role_id
224
+            WHERE sr.role_name like concat('%', #{roleName}, '%'))
224 225
         </if>
225 226
         <if test="cardNumber != null and cardNumber != ''">
226 227
             AND u.card_number like concat('%', #{cardNumber}, '%')