|
@@ -4,12 +4,22 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
+import org.springframework.security.jwt.crypto.sign.RsaVerifier;
|
|
|
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
|
|
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
|
|
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
|
|
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 java.io.IOException;
|
|
|
|
|
|
/**
|
|
|
* @Description: 权限资源配置类
|
|
@@ -18,6 +28,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableResourceServer
|
|
|
+@ComponentScan({"com.diagbot.config"})
|
|
|
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
|
|
|
Logger log = LoggerFactory.getLogger(ResourceServerConfigurer.class);
|
|
|
|
|
@@ -44,11 +55,28 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
|
|
|
|
|
|
@Override
|
|
|
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
|
|
|
- log.info("Configuring ResourceServerSecurityConfigurer ");
|
|
|
- resources.resourceId("user-service").tokenStore(tokenStore);
|
|
|
+ log.info("Configuring ResourceServerSecurityConfigurer");
|
|
|
+ resources.resourceId("user-service").tokenStore(new JwtTokenStore(jwtTokenEnhancerClient()));
|
|
|
}
|
|
|
|
|
|
@Autowired
|
|
|
- @Qualifier("tokenStoreClient")
|
|
|
- TokenStore tokenStore;
|
|
|
+ private CustomAccessTokenConverter customAccessTokenConverter;
|
|
|
+
|
|
|
+ @Bean("jwtTokenEnhancerClient")
|
|
|
+ protected JwtAccessTokenConverter jwtTokenEnhancerClient() {
|
|
|
+ JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
|
|
|
+ Resource resource = new ClassPathResource("public.cert");
|
|
|
+ String publicKey;
|
|
|
+ try {
|
|
|
+ publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ converter.setVerifierKey(publicKey);
|
|
|
+ //不设置这个会出现 Cannot convert access token to JSON
|
|
|
+ converter.setVerifier(new RsaVerifier(publicKey));
|
|
|
+ converter.setAccessTokenConverter(customAccessTokenConverter);
|
|
|
+ log.info("Created jwtTokenEnhancerClient");
|
|
|
+ return converter;
|
|
|
+ }
|
|
|
}
|