|
|
|
package com.begete.framework.config;
|
|
|
|
|
|
|
|
import com.begete.framework.config.properties.PermissionProperties;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
...
|
...
|
@@ -8,6 +9,7 @@ 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;
|
|
...
|
...
|
@@ -18,6 +20,9 @@ import com.begete.framework.security.filter.JwtAuthenticationTokenFilter; |
|
|
|
import com.begete.framework.security.handle.AuthenticationEntryPointImpl;
|
|
|
|
import com.begete.framework.security.handle.LogoutSuccessHandlerImpl;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* spring security配置
|
|
|
|
*
|
|
...
|
...
|
@@ -56,6 +61,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
|
|
|
@Autowired
|
|
|
|
private CorsFilter corsFilter;
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private PermissionProperties permissionProperties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 解决 无法直接注入 AuthenticationManager
|
|
|
|
*
|
|
...
|
...
|
@@ -87,7 +95,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
|
|
|
@Override
|
|
|
|
protected void configure(HttpSecurity httpSecurity) throws Exception
|
|
|
|
{
|
|
|
|
httpSecurity
|
|
|
|
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = httpSecurity
|
|
|
|
// CSRF禁用,因为不使用session
|
|
|
|
.csrf().disable()
|
|
|
|
// 认证失败处理类
|
|
...
|
...
|
@@ -104,7 +112,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
|
|
|
"/**/*.html",
|
|
|
|
"/**/*.css",
|
|
|
|
"/**/*.js"
|
|
|
|
).permitAll()
|
|
|
|
).permitAll();
|
|
|
|
|
|
|
|
expressionInterceptUrlRegistry
|
|
|
|
.antMatchers("/profile/**").anonymous()
|
|
|
|
.antMatchers("/common/download**").anonymous()
|
|
|
|
.antMatchers("/common/download/resource**").anonymous()
|
|
...
|
...
|
@@ -112,9 +122,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
|
|
|
.antMatchers("/swagger-resources/**").anonymous()
|
|
|
|
.antMatchers("/webjars/**").anonymous()
|
|
|
|
.antMatchers("/*/api-docs").anonymous()
|
|
|
|
.antMatchers("/druid/**").anonymous()
|
|
|
|
.antMatchers("/druid/**").anonymous();
|
|
|
|
|
|
|
|
List<String> excludeUrls = permissionProperties.getExcludeUrls();
|
|
|
|
if(excludeUrls != null)
|
|
|
|
excludeUrls.stream().forEach(e -> expressionInterceptUrlRegistry.antMatchers(e).anonymous());
|
|
|
|
|
|
|
|
// 除上面外的所有请求全部需要鉴权认证
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
expressionInterceptUrlRegistry.anyRequest().authenticated()
|
|
|
|
.and()
|
|
|
|
.headers().frameOptions().disable();
|
|
|
|
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
|
...
|
...
|
|