解决swagger-ui加了Oauth2后无法访问的问题

两种情况:

1.在客户端需要认证的地方,修改ResourceServerConfig的configure()方法

  1. package com.xuecheng.manage_course.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.core.io.Resource;
  6. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  10. import org.springframework.security.oauth2.provider.token.TokenStore;
  11. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  12. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStreamReader;
  16. import java.util.stream.Collectors;
  17. @Configuration
  18. @EnableResourceServer
  19. @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)//激活方法上的PreAuthorize注解
  20. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  21. //公钥
  22. private static final String PUBLIC_KEY = "publickey.txt";
  23. //定义JwtTokenStore,使用jwt令牌
  24. @Bean
  25. public TokenStore tokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) {
  26. return new JwtTokenStore(jwtAccessTokenConverter);
  27. }
  28. //定义JJwtAccessTokenConverter,使用jwt令牌
  29. @Bean
  30. public JwtAccessTokenConverter jwtAccessTokenConverter() {
  31. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  32. converter.setVerifierKey(getPubKey());
  33. return converter;
  34. }
  35. /**
  36. * 获取非对称加密公钥 Key
  37. * @return 公钥 Key
  38. */
  39. private String getPubKey() {
  40. Resource resource = new ClassPathResource(PUBLIC_KEY);
  41. try {
  42. InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream());
  43. BufferedReader br = new BufferedReader(inputStreamReader);
  44. return br.lines().collect(Collectors.joining("\n"));
  45. } catch (IOException ioe) {
  46. return null;
  47. }
  48. }
  49. //Http安全配置,对每个到达系统的http请求链接进行校验
  50. @Override
  51. public void configure(HttpSecurity http) throws Exception {
  52. //所有请求必须认证通过
  53. http.authorizeRequests()
  54. //下边的路径放行
  55. .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui",
  56. "/swagger-resources","/swagger-resources/configuration/security",
  57. "/swagger-ui.html","/course/coursebase/**").permitAll()
  58. .anyRequest().authenticated();
  59. }
  60. }

 

2.在授权认证的工程中则需要修改授权配置类WebSecurityConfig的configure方法:

针对swagger-ui的请求路径进行放行:需要放行静态资源

  1. package com.xuecheng.auth.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. /**
  7. * Created by mrt on 2018/5/18.
  8. */
  9. @Configuration
  10. @EnableWebSecurity
  11. class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  12. @Override
  13. public void configure(WebSecurity web) throws Exception {
  14. web.ignoring().antMatchers("/userlogin","/userlogout","/userjwt","/v2/api-docs", "/swagger-resources/configuration/ui",
  15. "/swagger-resources","/swagger-resources/configuration/security",
  16. "/swagger-ui.html","/css/**", "/js/**","/images/**", "/webjars/**", "**/favicon.ico", "/index");
  17. }
  18. }