|
@@ -1,6 +1,11 @@
|
|
|
package com.ruoyi.framework.config;
|
|
|
|
|
|
+import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
|
|
|
+import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
|
|
|
+import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
|
|
|
+import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.http.HttpMethod;
|
|
@@ -15,27 +20,25 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.filter.CorsFilter;
|
|
|
-import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
|
|
|
-import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
|
|
|
-import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
|
|
|
-import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* spring security配置
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
|
|
@Configuration
|
|
|
-public class SecurityConfig
|
|
|
-{
|
|
|
+public class SecurityConfig {
|
|
|
/**
|
|
|
* 自定义用户认证逻辑
|
|
|
*/
|
|
|
@Autowired
|
|
|
private UserDetailsService userDetailsService;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 认证失败处理类
|
|
|
*/
|
|
@@ -53,7 +56,7 @@ public class SecurityConfig
|
|
|
*/
|
|
|
@Autowired
|
|
|
private JwtAuthenticationTokenFilter authenticationTokenFilter;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 跨域过滤器
|
|
|
*/
|
|
@@ -66,12 +69,14 @@ public class SecurityConfig
|
|
|
@Autowired
|
|
|
private PermitAllUrlProperties permitAllUrl;
|
|
|
|
|
|
+ @Value("${huashe.permit.urlPatterns:[]}")
|
|
|
+ private List<String> urlPatterns;
|
|
|
+
|
|
|
/**
|
|
|
* 身份验证实现
|
|
|
*/
|
|
|
@Bean
|
|
|
- public AuthenticationManager authenticationManager()
|
|
|
- {
|
|
|
+ public AuthenticationManager authenticationManager() {
|
|
|
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
|
|
|
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
|
|
|
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
|
|
@@ -94,46 +99,48 @@ public class SecurityConfig
|
|
|
* authenticated | 用户登录后可访问
|
|
|
*/
|
|
|
@Bean
|
|
|
- protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
|
|
|
- {
|
|
|
+ protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
|
|
return httpSecurity
|
|
|
- // CSRF禁用,因为不使用session
|
|
|
- .csrf(csrf -> csrf.disable())
|
|
|
- // 禁用HTTP响应标头
|
|
|
- .headers((headersCustomizer) -> {
|
|
|
- headersCustomizer.cacheControl(cache -> cache.disable()).frameOptions(options -> options.sameOrigin());
|
|
|
- })
|
|
|
- // 认证失败处理类
|
|
|
- .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
|
|
|
- // 基于token,所以不需要session
|
|
|
- .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
- // 注解标记允许匿名访问的url
|
|
|
- .authorizeHttpRequests((requests) -> {
|
|
|
- permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
|
|
- // 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
|
|
- requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
|
|
|
- // 静态资源,可匿名访问
|
|
|
- .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
|
|
- .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
|
|
- // 除上面外的所有请求全部需要鉴权认证
|
|
|
- .anyRequest().authenticated();
|
|
|
- })
|
|
|
- // 添加Logout filter
|
|
|
- .logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
|
|
|
- // 添加JWT filter
|
|
|
- .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
|
|
|
- // 添加CORS filter
|
|
|
- .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
|
|
|
- .addFilterBefore(corsFilter, LogoutFilter.class)
|
|
|
- .build();
|
|
|
+ // CSRF禁用,因为不使用session
|
|
|
+ .csrf(csrf -> csrf.disable())
|
|
|
+ // 禁用HTTP响应标头
|
|
|
+ .headers((headersCustomizer) -> {
|
|
|
+ headersCustomizer.cacheControl(cache -> cache.disable()).frameOptions(options -> options.sameOrigin());
|
|
|
+ })
|
|
|
+ // 认证失败处理类
|
|
|
+ .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
|
|
|
+ // 基于token,所以不需要session
|
|
|
+ .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
+ // 注解标记允许匿名访问的url
|
|
|
+ .authorizeHttpRequests((requests) -> {
|
|
|
+ permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(urlPatterns)) {
|
|
|
+ requests.antMatchers(urlPatterns.toArray(new String[urlPatterns.size()])).permitAll();
|
|
|
+ }
|
|
|
+ // 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
|
|
+ requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
|
|
|
+ // 静态资源,可匿名访问
|
|
|
+ .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
|
|
+ .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
|
|
+ // 除上面外的所有请求全部需要鉴权认证
|
|
|
+ .anyRequest().authenticated();
|
|
|
+ })
|
|
|
+ // 添加Logout filter
|
|
|
+ .logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
|
|
|
+ // 添加JWT filter
|
|
|
+ .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
|
|
|
+ // 添加CORS filter
|
|
|
+ .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
|
|
|
+ .addFilterBefore(corsFilter, LogoutFilter.class)
|
|
|
+ .build();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 强散列哈希加密实现
|
|
|
*/
|
|
|
@Bean
|
|
|
- public BCryptPasswordEncoder bCryptPasswordEncoder()
|
|
|
- {
|
|
|
+ public BCryptPasswordEncoder bCryptPasswordEncoder() {
|
|
|
return new BCryptPasswordEncoder();
|
|
|
}
|
|
|
}
|