package com.dhcc.finance.config; import java.io.IOException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; import org.springframework.util.FileCopyUtils; import com.dhcc.common.constant.SysConstants; @Configuration public class JwtConfig { @Autowired ResourceServerProperties resourceServerProperties; /** * * @Title: jwtAccessTokenConverter * @Description: 资源服务器JwtAccessToken转换器,从本地拉取公钥 * @return JwtAccessTokenConverter * @author chenrui * @date 2019年9月14日 下午9:22:41 * @modifyLog: */ @Bean public JwtAccessTokenConverter jwtAccessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); Resource resource = new ClassPathResource(SysConstants.JwtConfig.JWT_PUBLICKEY_CER); String publicKey; try { publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream())); }catch (IOException e) { throw new RuntimeException(e); } converter.setVerifierKey(publicKey); // 设置公钥 return converter; } /** * * @Title: tokenStore * @Description: 资源服务器token仓库 * @param jwtAccessTokenConverter * @return TokenStore * @author chenrui * @date 2019年9月14日 下午9:24:29 * @modifyLog: */ @Bean public TokenStore jwtTokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) { return new JwtTokenStore(jwtAccessTokenConverter); } /** * * @Title: jwtTokenServices * @Description: * @param jwtTokenStore * @return DefaultTokenServices * @author chenrui * @date 2019年9月16日 下午2:05:47 * @modifyLog: */ @Bean public DefaultTokenServices jwtTokenServices(TokenStore jwtTokenStore) { DefaultTokenServices services = new DefaultTokenServices(); services.setTokenStore(jwtTokenStore); return services; } }