ResourceServerConfigurer.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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("/oauth/token").permitAll()
  40. .antMatchers("/oauth/check_token").permitAll()
  41. .antMatchers("/tran/lisConfig/isExistRecord").permitAll()
  42. .antMatchers("/tran/lisConfig/saveOrUpdateRecord").permitAll()
  43. .antMatchers("/tran/lisConfig/saveOrUpdateRecords").permitAll()
  44. .antMatchers("/tran/lisConfig/deleteRecord").permitAll()
  45. .antMatchers("/tran/lisConfig/deleteRecords").permitAll()
  46. .antMatchers("/tran/lisConfig/getPage").permitAll()
  47. .antMatchers("/tran/lisConfig/importExcel").permitAll()
  48. .antMatchers("/tran/lisConfig/exportExcel").permitAll()
  49. .antMatchers("/tran/pacsConfig/isExistRecord").permitAll()
  50. .antMatchers("/tran/pacsConfig/saveOrUpdateRecord").permitAll()
  51. .antMatchers("/tran/pacsConfig/saveOrUpdateRecords").permitAll()
  52. .antMatchers("/tran/pacsConfig/deleteRecord").permitAll()
  53. .antMatchers("/tran/pacsConfig/deleteRecords").permitAll()
  54. .antMatchers("/tran/pacsConfig/getPage").permitAll()
  55. .antMatchers("/tran/pacsConfig/importExcel").permitAll()
  56. .antMatchers("/tran/pacsConfig/exportExcel").permitAll()
  57. .antMatchers("/tran/diseaseConfig/isExistRecord").permitAll()
  58. .antMatchers("/tran/diseaseConfig/saveOrUpdateRecord").permitAll()
  59. .antMatchers("/tran/diseaseConfig/saveOrUpdateRecords").permitAll()
  60. .antMatchers("/tran/diseaseConfig/deleteRecord").permitAll()
  61. .antMatchers("/tran/diseaseConfig/deleteRecords").permitAll()
  62. .antMatchers("/tran/diseaseConfig/getPage").permitAll()
  63. .antMatchers("/tran/diseaseConfig/importExcel").permitAll()
  64. .antMatchers("/tran/diseaseConfig/exportExcel").permitAll()
  65. .antMatchers("/tran/drugConfig/isExistRecord").permitAll()
  66. .antMatchers("/tran/drugConfig/saveOrUpdateRecord").permitAll()
  67. .antMatchers("/tran/drugConfig/saveOrUpdateRecords").permitAll()
  68. .antMatchers("/tran/drugConfig/deleteRecord").permitAll()
  69. .antMatchers("/tran/drugConfig/deleteRecords").permitAll()
  70. .antMatchers("/tran/drugConfig/getPage").permitAll()
  71. .antMatchers("/tran/drugConfig/importExcel").permitAll()
  72. .antMatchers("/tran/drugConfig/exportExcel").permitAll()
  73. .antMatchers("/tran/operationConfig/isExistRecord").permitAll()
  74. .antMatchers("/tran/operationConfig/saveOrUpdateRecord").permitAll()
  75. .antMatchers("/tran/operationConfig/saveOrUpdateRecords").permitAll()
  76. .antMatchers("/tran/operationConfig/deleteRecord").permitAll()
  77. .antMatchers("/tran/operationConfig/deleteRecords").permitAll()
  78. .antMatchers("/tran/operationConfig/getPage").permitAll()
  79. .antMatchers("/tran/operationConfig/importExcel").permitAll()
  80. .antMatchers("/tran/operationConfig/exportExcel").permitAll()
  81. .antMatchers("/sys/versionInfo/getVersionInfoAlls").permitAll()
  82. .antMatchers("/sys/disclaimerInfo/getDisclaimerInfo").permitAll()
  83. .antMatchers("/sys/mr/createMr").permitAll()
  84. .antMatchers("/sys/mr/getMr").permitAll()
  85. .antMatchers("/sys/plan/getSysPlanInfoDatas").permitAll()
  86. .antMatchers("/sys/mrqc/analyze_run").permitAll()
  87. .antMatchers("/sys/tokenPermission/delPermission").permitAll()
  88. .antMatchers("/sys/tokenPermission/getPermission").permitAll()
  89. .antMatchers("/push/push").permitAll()
  90. .antMatchers("/push/indicationPush").permitAll()
  91. .antMatchers("/sys/plan/getPlanInfoPages").permitAll()
  92. .antMatchers("/sys/plan/savePlanInfoDatas").permitAll()
  93. .antMatchers("/sys/plan/getSysPlanInfoDatas").permitAll()
  94. .antMatchers("/sys/plan/cancelPlanDatas").permitAll()
  95. .antMatchers("/sys/plan/revStopPlans").permitAll()
  96. .antMatchers("/**").authenticated();
  97. // .antMatchers("/**").permitAll();
  98. }
  99. @Override
  100. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  101. log.info("Configuring ResourceServerSecurityConfigurer");
  102. resources.resourceId("user-service").tokenStore(new JwtTokenStore(jwtTokenEnhancerClient()));
  103. }
  104. @Autowired
  105. private CustomAccessTokenConverter customAccessTokenConverter;
  106. @Bean("jwtTokenEnhancerClient")
  107. protected JwtAccessTokenConverter jwtTokenEnhancerClient() {
  108. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  109. Resource resource = new ClassPathResource("public.cert");
  110. String publicKey;
  111. try {
  112. publicKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
  113. } catch (IOException e) {
  114. throw new RuntimeException(e);
  115. }
  116. converter.setVerifierKey(publicKey);
  117. //不设置这个会出现 Cannot convert access token to JSON
  118. converter.setVerifier(new RsaVerifier(publicKey));
  119. converter.setAccessTokenConverter(customAccessTokenConverter);
  120. log.info("Created jwtTokenEnhancerClient success");
  121. return converter;
  122. }
  123. }