io.github.dengchen2020.security.config.StatelessTokenAutoConfiguration 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.JwtTokenServiceImpl;
import io.github.dengchen2020.security.core.support.token.TokenService;
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认证自动配置
* @author dengchen
* @since 2024/7/3
*/
@ConditionalOnProperty(value = "dc.security.token.stateless", matchIfMissing = true, havingValue = "true")
@Configuration(proxyBeanMethods = false)
public class StatelessTokenAutoConfiguration {
private final SecurityProperties securityProperties;
public StatelessTokenAutoConfiguration(SecurityProperties securityProperties) {
this.securityProperties = securityProperties;
}
@ConditionalOnProperty("dc.security.jwt.secret")
@ConditionalOnMissingBean
@Bean
public JwtHelper jwtHelper() {
return new JwtHelper(securityProperties.getJwt().getSecret());
}
@ConditionalOnMissingBean
@Bean
public TokenService tokenService() {
SecurityProperties.Token token = securityProperties.getToken();
return new JwtTokenServiceImpl(token::getPrincipalClass, token.getExpireSeconds());
}
}