2020-03-07 14:40:15 +08:00
|
|
|
|
package com.pj.satoken;
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
2020-06-15 22:03:41 +08:00
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
2020-03-07 14:40:15 +08:00
|
|
|
|
|
2020-09-07 02:21:35 +08:00
|
|
|
|
import cn.dev33.satoken.config.SaTokenConfig;
|
2021-01-11 20:08:17 +08:00
|
|
|
|
import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
|
2020-03-07 14:40:15 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* sa-token代码方式进行配置
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
2020-06-15 22:03:41 +08:00
|
|
|
|
public class MySaTokenConfig implements WebMvcConfigurer {
|
2020-03-07 14:40:15 +08:00
|
|
|
|
|
2020-12-14 01:34:11 +08:00
|
|
|
|
// 获取配置Bean (以代码的方式配置sa-token, 此配置会覆盖yml中的配置 )
|
2020-12-17 02:19:56 +08:00
|
|
|
|
// @Primary
|
|
|
|
|
// @Bean(name="MySaTokenConfig")
|
2020-09-07 02:21:35 +08:00
|
|
|
|
public SaTokenConfig getSaTokenConfig() {
|
|
|
|
|
SaTokenConfig config = new SaTokenConfig();
|
|
|
|
|
config.setTokenName("satoken"); // token名称 (同时也是cookie名称)
|
|
|
|
|
config.setTimeout(30 * 24 * 60 * 60); // token有效期,单位s 默认30天
|
2021-01-02 04:00:49 +08:00
|
|
|
|
config.setActivityTimeout(-1); // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
|
|
|
|
|
config.setAllowConcurrentLogin(true); // 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
|
|
|
|
config.setIsShare(true); // 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
2020-12-17 02:19:56 +08:00
|
|
|
|
config.setTokenStyle("uuid"); // token风格
|
2020-09-07 02:21:35 +08:00
|
|
|
|
return config;
|
|
|
|
|
}
|
2020-03-07 14:40:15 +08:00
|
|
|
|
|
|
|
|
|
// 注册sa-token的拦截器,打开注解式鉴权功能
|
|
|
|
|
@Override
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
2021-01-11 20:08:17 +08:00
|
|
|
|
// 注册注解拦截器
|
2021-01-13 02:00:47 +08:00
|
|
|
|
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
|
2020-03-07 14:40:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 15:19:55 +08:00
|
|
|
|
|
2020-03-07 14:40:15 +08:00
|
|
|
|
}
|