ResourceServerConfigurer.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.diagbot.config;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.ComponentScan;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.core.io.ClassPathResource;
  9. import org.springframework.core.io.Resource;
  10. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  11. import org.springframework.security.jwt.crypto.sign.RsaVerifier;
  12. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  13. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  15. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  16. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  17. import org.springframework.util.FileCopyUtils;
  18. import java.io.IOException;
  19. /**
  20. * @Description: 权限资源配置类
  21. * @author: gaodm
  22. * @time: 2018/8/2 14:21
  23. */
  24. @Configuration
  25. @EnableResourceServer
  26. @ComponentScan({"com.diagbot.config"})
  27. public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
  28. Logger log = LoggerFactory.getLogger(ResourceServerConfigurer.class);
  29. @Override
  30. public void configure(HttpSecurity http) throws Exception {
  31. http.cors()
  32. .and()
  33. .csrf().disable()
  34. .authorizeRequests()
  35. .regexMatchers(".*swagger.*", ".*v2.*", ".*webjars.*", "/druid.*", "/actuator.*", "/hystrix.*").permitAll()
  36. .antMatchers("/sys/user/getJwt").permitAll()
  37. .antMatchers("/sys/user/refreshJwt").permitAll()
  38. .antMatchers("/sys/user/checkToken").permitAll()
  39. .antMatchers("/sys/dictionaryInfo/getDictionary").permitAll()
  40. .antMatchers("/oauth/token").permitAll()
  41. .antMatchers("/oauth/check_token").permitAll()
  42. .antMatchers("/cache/clear").permitAll()
  43. .antMatchers("/qc/behospitalInfo/execule").permitAll()
  44. .antMatchers("/qc/behospitalInfo/analyze_rpc").permitAll()
  45. .antMatchers("/qc/behospitalInfo/analyze_api").permitAll()
  46. .antMatchers("/qc/module/getById").permitAll()
  47. .antMatchers("/**").authenticated();
  48. // .antMatchers("/**").permitAll();
  49. }
  50. @Override
  51. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  52. log.info("Configuring ResourceServerSecurityConfigurer");
  53. resources.resourceId("user-service").tokenStore(new JwtTokenStore(jwtTokenEnhancerClient()));
  54. }
  55. @Autowired
  56. private CustomAccessTokenConverter customAccessTokenConverter;
  57. @Bean("jwtTokenEnhancerClient")
  58. protected JwtAccessTokenConverter jwtTokenEnhancerClient() {
  59. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  60. Resource resource = new ClassPathResource("public.cert");
  61. String publicKey;
  62. try {
  63. publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
  64. } catch (IOException e) {
  65. throw new RuntimeException(e);
  66. }
  67. converter.setVerifierKey(publicKey);
  68. //不设置这个会出现 Cannot convert access token to JSON
  69. converter.setVerifier(new RsaVerifier(publicKey));
  70. converter.setAccessTokenConverter(customAccessTokenConverter);
  71. log.info("Created jwtTokenEnhancerClient success");
  72. return converter;
  73. }
  74. }