一、导入依赖
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
二、yml文件添加配置
# token配置
token:
# 令牌自定义标识
header: Authorization
# 令牌秘钥
secret: abcdefghijklmnopqrstuvwxyz
# 令牌有效期(默认30分钟)
expireTime: 30
三、编写SecurityConfig
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Qualifier("UserDetailService")
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationEntryPointImpl unauthorizedHandler;
@Autowired
private LogoutSuccessHandlerImpl logoutSuccessHandler;
@Autowired
private JwtAuthenticationTokenFilter authenticationTokenFilter;
/**
* 配置filter链
* @param web
* @throws Exception
*/
@Override
public void configure(WebSecurity web) throws Exception {
//绕过所有的filter
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
}
/**
* 配置拦截保护的请求,哪些请求需要去验证,哪些请求放行
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
//anonymous()允许匿名访问
//permitAll(