|
|
@@ -15,6 +15,9 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
15
|
15
|
import org.springframework.security.web.SecurityFilterChain;
|
|
16
|
16
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
17
|
17
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
|
|
18
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
19
|
+import org.springframework.web.cors.CorsConfigurationSource;
|
|
|
20
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
18
|
21
|
import org.springframework.web.filter.CorsFilter;
|
|
19
|
22
|
import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
|
|
20
|
23
|
import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
|
|
|
@@ -23,7 +26,7 @@ import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
23
|
26
|
|
|
24
|
27
|
/**
|
|
25
|
28
|
* spring security配置
|
|
26
|
|
- *
|
|
|
29
|
+ *
|
|
27
|
30
|
* @author ruoyi
|
|
28
|
31
|
*/
|
|
29
|
32
|
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true)
|
|
|
@@ -35,7 +38,7 @@ public class SecurityConfig
|
|
35
|
38
|
*/
|
|
36
|
39
|
@Autowired
|
|
37
|
40
|
private UserDetailsService userDetailsService;
|
|
38
|
|
-
|
|
|
41
|
+
|
|
39
|
42
|
/**
|
|
40
|
43
|
* 认证失败处理类
|
|
41
|
44
|
*/
|
|
|
@@ -53,7 +56,7 @@ public class SecurityConfig
|
|
53
|
56
|
*/
|
|
54
|
57
|
@Autowired
|
|
55
|
58
|
private JwtAuthenticationTokenFilter authenticationTokenFilter;
|
|
56
|
|
-
|
|
|
59
|
+
|
|
57
|
60
|
/**
|
|
58
|
61
|
* 跨域过滤器
|
|
59
|
62
|
*/
|
|
|
@@ -94,54 +97,72 @@ public class SecurityConfig
|
|
94
|
97
|
* authenticated | 用户登录后可访问
|
|
95
|
98
|
*/
|
|
96
|
99
|
@Bean
|
|
97
|
|
- protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
|
|
98
|
|
- {
|
|
99
|
|
- 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();
|
|
|
100
|
+ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
101
|
+ // 添加公开API到白名单
|
|
|
102
|
+ permitAllUrl.getUrls().add("/category/**");
|
|
|
103
|
+ permitAllUrl.getUrls().add("/novel/**");
|
|
|
104
|
+ permitAllUrl.getUrls().add("/ad/**");
|
|
|
105
|
+ permitAllUrl.getUrls().add("/swagger-ui/**");
|
|
|
106
|
+ permitAllUrl.getUrls().add("/v3/api-docs/**");
|
|
|
107
|
+
|
|
|
108
|
+ http
|
|
|
109
|
+ // 禁用CSRF
|
|
|
110
|
+ .csrf(csrf -> csrf.disable())
|
|
|
111
|
+ // 设置CORS配置
|
|
|
112
|
+ .cors(cors -> cors.configurationSource(corsConfigurationSource()))
|
|
|
113
|
+ // 异常处理
|
|
|
114
|
+ .exceptionHandling(exception ->
|
|
|
115
|
+ exception.authenticationEntryPoint(unauthorizedHandler)
|
|
|
116
|
+ )
|
|
|
117
|
+ // 无状态会话
|
|
|
118
|
+ .sessionManagement(session ->
|
|
|
119
|
+ session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
|
120
|
+ )
|
|
|
121
|
+ // 权限配置
|
|
|
122
|
+ .authorizeHttpRequests(auth -> auth
|
|
|
123
|
+ .antMatchers(permitAllUrl.getUrls().toArray(new String[0])).permitAll()
|
|
|
124
|
+ .antMatchers("/login", "/register", "/captchaImage").permitAll()
|
|
|
125
|
+ .antMatchers(HttpMethod.GET,
|
|
|
126
|
+ "/",
|
|
|
127
|
+ "/*.html",
|
|
|
128
|
+ "/**/*.html",
|
|
|
129
|
+ "/**/*.css",
|
|
|
130
|
+ "/**/*.js",
|
|
|
131
|
+ "/profile/**"
|
|
|
132
|
+ ).permitAll()
|
|
|
133
|
+ .antMatchers(
|
|
|
134
|
+ "/swagger-ui.html",
|
|
|
135
|
+ "/swagger-resources/**",
|
|
|
136
|
+ "/webjars/**",
|
|
|
137
|
+ "/*/api-docs",
|
|
|
138
|
+ "/druid/**"
|
|
|
139
|
+ ).permitAll()
|
|
|
140
|
+ .anyRequest().authenticated()
|
|
|
141
|
+ )
|
|
|
142
|
+ // 登出配置
|
|
|
143
|
+ .logout(logout ->
|
|
|
144
|
+ logout.logoutUrl("/logout")
|
|
|
145
|
+ .logoutSuccessHandler(logoutSuccessHandler)
|
|
|
146
|
+ )
|
|
|
147
|
+ // 添加JWT过滤器
|
|
|
148
|
+ .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
|
|
|
149
|
+ // 添加CORS过滤器
|
|
|
150
|
+ .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
|
|
|
151
|
+
|
|
|
152
|
+ return http.build();
|
|
129
|
153
|
}
|
|
130
|
|
- // SecurityConfig.java
|
|
131
|
154
|
|
|
|
155
|
+ // CORS配置
|
|
|
156
|
+ private CorsConfigurationSource corsConfigurationSource() {
|
|
|
157
|
+ CorsConfiguration configuration = new CorsConfiguration();
|
|
|
158
|
+ configuration.addAllowedOrigin("*"); // 允许所有来源
|
|
|
159
|
+ configuration.addAllowedMethod("*"); // 允许所有方法
|
|
|
160
|
+ configuration.addAllowedHeader("*"); // 允许所有头部
|
|
|
161
|
+ configuration.setAllowCredentials(true); // 允许凭证
|
|
132
|
162
|
|
|
133
|
|
- protected void configure(HttpSecurity http) throws Exception {
|
|
134
|
|
- http
|
|
135
|
|
- .authorizeRequests()
|
|
136
|
|
- // 放行公共API
|
|
137
|
|
- .antMatchers("/novel/list").permitAll()
|
|
138
|
|
- .antMatchers("/category/tree").permitAll()
|
|
139
|
|
- .antMatchers("/novel/hot").permitAll()
|
|
140
|
|
- .antMatchers("/ad/position").permitAll()
|
|
141
|
|
- // 其他请求需要认证
|
|
142
|
|
- .anyRequest().authenticated()
|
|
143
|
|
- .and()
|
|
144
|
|
- .csrf().disable();
|
|
|
163
|
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
164
|
+ source.registerCorsConfiguration("/**", configuration);
|
|
|
165
|
+ return source;
|
|
145
|
166
|
}
|
|
146
|
167
|
/**
|
|
147
|
168
|
* 强散列哈希加密实现
|