55import com .github .cadecode .uniboot .framework .api .config .SecurityConfig .SecurityProperties ;
66import com .github .cadecode .uniboot .framework .api .enums .AuthModelEnum ;
77import com .github .cadecode .uniboot .framework .api .security .filter .TokenAuthFilter ;
8- import com .github .cadecode .uniboot .framework .api .security .handler .*;
8+ import com .github .cadecode .uniboot .framework .api .security .handler .NoAuthenticationHandler ;
9+ import com .github .cadecode .uniboot .framework .api .security .handler .NoAuthorityHandler ;
910import com .github .cadecode .uniboot .framework .api .security .voter .DataBaseRoleVoter ;
1011import lombok .Data ;
1112import lombok .RequiredArgsConstructor ;
1213import lombok .extern .slf4j .Slf4j ;
14+ import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
1315import org .springframework .boot .context .properties .ConfigurationProperties ;
1416import org .springframework .boot .context .properties .EnableConfigurationProperties ;
15- import org .springframework .context .annotation .Bean ;
1617import org .springframework .context .annotation .Configuration ;
1718import org .springframework .http .HttpMethod ;
1819import org .springframework .security .access .vote .UnanimousBased ;
19- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
2020import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
2121import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
2222import org .springframework .security .config .annotation .web .builders .WebSecurity ;
2323import org .springframework .security .config .annotation .web .builders .WebSecurity .IgnoredRequestConfigurer ;
2424import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
2525import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
2626import org .springframework .security .config .http .SessionCreationPolicy ;
27- import org .springframework .security .core .userdetails .UserDetailsService ;
28- import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
29- import org .springframework .security .crypto .password .PasswordEncoder ;
3027import org .springframework .security .web .access .expression .WebExpressionVoter ;
3128import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
3229
4037 * @date 2022/5/27
4138 */
4239@ Slf4j
43- @ Data
4440@ RequiredArgsConstructor
4541@ EnableWebSecurity
4642@ EnableGlobalMethodSecurity (prePostEnabled = true )
4743@ EnableConfigurationProperties (SecurityProperties .class )
44+ @ ConditionalOnMissingBean (SecurityConfig .class )
4845@ Configuration
49- public class SecurityConfig {
50-
51- /**
52- * 登录路径
53- */
54- public static final String LOGIN_URL = "/login" ;
55-
56- /**
57- * 登录参数
58- */
59- public static final String USERNAME_PARAMETER = "username" ;
60- public static final String PASSWORD_PARAMETER = "password" ;
61-
62- /**
63- * 注销路径
64- */
65- public static final String LOGOUT_URL = "/logout" ;
46+ public class SecurityConfig extends WebSecurityConfigurerAdapter {
6647
6748 /**
6849 * 配置项
@@ -72,11 +53,8 @@ public class SecurityConfig {
7253 /**
7354 * 注入各种处理器
7455 */
75- private final LoginSuccessHandler loginSuccessHandler ;
76- private final LoginFailureHandler loginFailureHandler ;
7756 private final NoAuthenticationHandler noAuthenticationHandler ;
7857 private final NoAuthorityHandler noAuthorityHandler ;
79- private final SignOutSuccessHandler signOutSuccessHandler ;
8058
8159 /**
8260 * 注入 Token 过滤器
@@ -88,88 +66,50 @@ public class SecurityConfig {
8866 */
8967 private final DataBaseRoleVoter dataBaseRoleVoter ;
9068
91- /**
92- * 注入 UserDetailsService
93- */
94- private final UserDetailsService userDetailsService ;
95-
96- /**
97- * 密码加密器
98- */
99- @ Bean
100- public PasswordEncoder passwordEncoder () {
101- return new BCryptPasswordEncoder ();
69+ @ Override
70+ protected void configure (HttpSecurity http ) throws Exception {
71+ // 关闭 csrf
72+ http .csrf ().disable ();
73+ // 关闭 session 管理
74+ http .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS );
75+ // 配置鉴权规则
76+ http .authorizeRequests ()
77+ // 尝试请求直接通过
78+ .antMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
79+ .anyRequest ().authenticated ();
80+ // 配置异常处理
81+ http .exceptionHandling ()
82+ // 配置未登录处理器
83+ .authenticationEntryPoint (noAuthenticationHandler )
84+ // 配置无权限处理器
85+ .accessDeniedHandler (noAuthorityHandler );
86+ // 自定义的 accessDecisionManager
87+ http .authorizeRequests ()
88+ .accessDecisionManager (new UnanimousBased (
89+ Arrays .asList (new WebExpressionVoter (), dataBaseRoleVoter )));
90+ // 配置 Token 校验过滤器
91+ http .addFilterBefore (tokenAuthFilter , UsernamePasswordAuthenticationFilter .class );
92+ log .info ("Config Security over,AuthModel:{}" , properties .getAuthModel ());
10293 }
10394
104- /**
105- * Security 配置
106- */
107- @ Bean
108- public WebSecurityConfigurerAdapter webSecurityConfigurer (PasswordEncoder passwordEncoder ) {
109- return new WebSecurityConfigurerAdapter () {
110-
111- @ Override
112- protected void configure (AuthenticationManagerBuilder auth ) throws Exception {
113- auth .userDetailsService (userDetailsService ).passwordEncoder (passwordEncoder );
114- }
115-
116- @ Override
117- protected void configure (HttpSecurity http ) throws Exception {
118- // 关闭 csrf
119- http .csrf ().disable ();
120- // 关闭 session 管理
121- http .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS );
122- // 配置鉴权规则
123- http .authorizeRequests ()
124- // 尝试请求直接通过
125- .antMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
126- .anyRequest ().authenticated ();
127- // 配置注销处理器
128- http .logout ().permitAll ()
129- .logoutUrl (LOGOUT_URL )
130- .logoutSuccessHandler (signOutSuccessHandler );
131- // 配置异常处理
132- http .exceptionHandling ()
133- // 配置未登录处理器
134- .authenticationEntryPoint (noAuthenticationHandler )
135- // 配置无权限处理器
136- .accessDeniedHandler (noAuthorityHandler );
137- // 自定义的 accessDecisionManager
138- http .authorizeRequests ()
139- .accessDecisionManager (new UnanimousBased (
140- Arrays .asList (new WebExpressionVoter (), dataBaseRoleVoter )));
141- // 配置登录处理器
142- http .formLogin ().permitAll ()
143- .loginProcessingUrl (LOGIN_URL )
144- .usernameParameter (USERNAME_PARAMETER )
145- .passwordParameter (PASSWORD_PARAMETER )
146- .successHandler (loginSuccessHandler )
147- .failureHandler (loginFailureHandler );
148- // 配置 Token 校验过滤器
149- http .addFilterBefore (tokenAuthFilter , UsernamePasswordAuthenticationFilter .class );
150- log .info ("Config Security over,AuthModel:{}" , properties .getAuthModel ());
151- }
152-
153- @ Override
154- public void configure (WebSecurity web ) {
155- // 忽略配置器
156- IgnoredRequestConfigurer ignoring = web .ignoring ();
157- // 放行 swagger knife 文档
158- ignoring .antMatchers ("/doc.html" , "/webjars/**" , "/swagger-resources/**" , "/v2/api-docs/**" );
159- // 放行其他框架
160- ignoring .antMatchers ("/error" , "/druid/**" , "/actuator/**" );
161- // 设置忽略的路径
162- List <String > ignoreUrls = properties .getIgnoreUrls ();
163- if (CollUtil .isNotEmpty (ignoreUrls )) {
164- log .info ("Config Security ignore urls:{}" , ignoreUrls );
165- ignoring .antMatchers (ArrayUtil .toArray (ignoreUrls , String .class ));
166- }
167- }
168- };
95+ @ Override
96+ public void configure (WebSecurity web ) {
97+ // 忽略配置器
98+ IgnoredRequestConfigurer ignoring = web .ignoring ();
99+ // 放行 swagger knife 文档
100+ ignoring .antMatchers ("/doc.html" , "/webjars/**" , "/swagger-resources/**" , "/v2/api-docs/**" );
101+ // 放行其他框架
102+ ignoring .antMatchers ("/error" , "/druid/**" , "/actuator/**" );
103+ // 设置忽略的路径
104+ List <String > ignoreUrls = properties .getIgnoreUrls ();
105+ if (CollUtil .isNotEmpty (ignoreUrls )) {
106+ log .info ("Config Security ignore urls:{}" , ignoreUrls );
107+ ignoring .antMatchers (ArrayUtil .toArray (ignoreUrls , String .class ));
108+ }
169109 }
170110
171111 /**
172- * Security 配置
112+ * Security 配置项
173113 */
174114 @ Data
175115 @ ConfigurationProperties ("uni-boot.security" )
@@ -188,12 +128,11 @@ public static class SecurityProperties {
188128 /**
189129 * JWT Token 配置
190130 */
191- private TokenConfig token ;
192-
131+ private TokenConfig tokenConfig ;
193132 }
194133
195134 /**
196- * JWT 配置类
135+ * JWT 配置项
197136 */
198137 @ Data
199138 public static class TokenConfig {
0 commit comments