io.github.dengchen2020.security.config.TokenAutoConfiguration Maven / Gradle / Ivy
package io.github.dengchen2020.security.config;
import io.github.dengchen2020.security.core.support.token.JwtHelper;
import io.github.dengchen2020.security.core.support.token.TokenService;
import io.github.dengchen2020.security.core.support.token.TokenServiceImpl;
import io.github.dengchen2020.security.properties.SecurityProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 有状态Token认证自动配置
* 依赖redis,需引入
* {@code
*
* org.springframework.boot
* spring-boot-starter-data-redis
* latest
* }
* @author dengchen
* @since 2024/4/28
*/
@ConditionalOnProperty(value = "dc.security.token.stateless", havingValue = "false")
@Configuration(proxyBeanMethods = false)
public class TokenAutoConfiguration {
private final SecurityProperties securityProperties;
public TokenAutoConfiguration(SecurityProperties securityProperties) {
this.securityProperties = securityProperties;
}
@ConditionalOnMissingBean
@Bean
public TokenService tokenService() {
SecurityProperties.Token token = securityProperties.getToken();
return new TokenServiceImpl(token::getPrincipalClass, token.getExpireSeconds(), token.getMaxOnlineNum(), token.isEnabledRefresh());
}
@ConditionalOnProperty("dc.security.jwt.secret")
@ConditionalOnMissingBean
@Bean
public JwtHelper jwtHelper() {
return new JwtHelper(securityProperties.getJwt().getSecret());
}
}