|
@@ -8,12 +8,14 @@ import org.springframework.security.config.annotation.authentication.builders.Au
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
+import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
|
|
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;
|
|
@@ -55,7 +57,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
*/
|
|
|
@Autowired
|
|
|
private CorsFilter corsFilter;
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 允许匿名访问的地址
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ private PermitAllUrlProperties permitAllUrl;
|
|
|
+
|
|
|
/**
|
|
|
* 解决 无法直接注入 AuthenticationManager
|
|
|
*
|
|
@@ -87,6 +95,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
@Override
|
|
|
protected void configure(HttpSecurity httpSecurity) throws Exception
|
|
|
{
|
|
|
+ // 注解标记允许匿名访问的url
|
|
|
+ ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
|
|
|
+ permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());
|
|
|
+
|
|
|
httpSecurity
|
|
|
// CSRF禁用,因为不使用session
|
|
|
.csrf().disable()
|
|
@@ -98,24 +110,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|
|
.authorizeRequests()
|
|
|
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
|
|
.antMatchers("/login", "/register", "/captchaImage").anonymous()
|
|
|
- .antMatchers(
|
|
|
- HttpMethod.GET,
|
|
|
- "/",
|
|
|
- "/*.html",
|
|
|
- "/**/*.html",
|
|
|
- "/**/*.css",
|
|
|
- "/**/*.js",
|
|
|
- "/profile/**"
|
|
|
- ).permitAll()
|
|
|
- .antMatchers("/swagger-ui.html").anonymous()
|
|
|
- .antMatchers("/swagger-resources/**").anonymous()
|
|
|
- .antMatchers("/webjars/**").anonymous()
|
|
|
- .antMatchers("/*/api-docs").anonymous()
|
|
|
- .antMatchers("/druid/**").anonymous()
|
|
|
+ // 静态资源,可匿名访问
|
|
|
+ .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
|
|
+ .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
|
|
// 除上面外的所有请求全部需要鉴权认证
|
|
|
.anyRequest().authenticated()
|
|
|
.and()
|
|
|
.headers().frameOptions().disable();
|
|
|
+ // 添加Logout filter
|
|
|
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
|
|
// 添加JWT filter
|
|
|
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|