ResourceServerConfig.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.yihu.base.security.config;
  2. import com.yihu.base.security.properties.SecurityProperties;
  3. import com.yihu.base.security.rbas.UserServiceProvider;
  4. import com.yihu.base.security.rbas.provider.AuthorizeConfigProviderManager;
  5. import com.yihu.base.security.rbas.provider.UserNamePasswordAuthenticationProvider;
  6. import com.yihu.base.security.sms.SmsCodeAuthenticationSecurityConfig;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Primary;
  13. import org.springframework.security.authentication.AuthenticationProvider;
  14. import org.springframework.security.authentication.ProviderManager;
  15. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  16. import org.springframework.security.authentication.dao.ReflectionSaltSource;
  17. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  18. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  19. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  20. import org.springframework.security.crypto.password.PasswordEncoder;
  21. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  22. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  23. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  24. import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
  25. import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;
  26. import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
  27. import org.springframework.security.oauth2.provider.token.TokenStore;
  28. import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
  29. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  30. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  31. import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
  32. import org.springframework.util.Base64Utils;
  33. import javax.inject.Inject;
  34. import java.util.ArrayList;
  35. import java.util.Arrays;
  36. /**
  37. * Created by chenweida on 2017/12/4.
  38. */
  39. @Configuration
  40. @EnableResourceServer //开启资源服务器
  41. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  42. @Autowired
  43. protected AuthenticationSuccessHandler authenticationSuccessHandler;
  44. @Autowired
  45. protected AuthenticationFailureHandler authenticationFailureHandler;
  46. @Autowired
  47. private OAuth2AuthenticationManager authenticationManager;
  48. @Autowired
  49. private TokenStore redisTokenStore;
  50. @Autowired
  51. private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;
  52. @Autowired
  53. private AuthorizeConfigProviderManager authorizeConfigProviderManager;
  54. @Autowired
  55. private OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler;
  56. @Autowired
  57. private LogoutSuccessHandler baseLogoutSuccessHandler;
  58. @Autowired
  59. private UserServiceProvider userServiceProvider;
  60. @Autowired
  61. private PasswordEncoder passwordEncoder;
  62. @Override
  63. public void configure(HttpSecurity http) throws Exception {
  64. http
  65. .csrf().disable()
  66. .formLogin()//设置 账号密码登陆
  67. .loginPage(SecurityProperties.formLoginPage)
  68. .loginProcessingUrl(SecurityProperties.formLogin)
  69. .usernameParameter("username")//默认就是username
  70. .passwordParameter("password")//默认就是password
  71. .successHandler(authenticationSuccessHandler)
  72. .failureHandler(authenticationFailureHandler)
  73. .and()
  74. .logout().logoutUrl(SecurityProperties.formLoginout).logoutSuccessUrl("/")
  75. .logoutSuccessHandler(baseLogoutSuccessHandler)
  76. .and()
  77. .apply(smsCodeAuthenticationSecurityConfig); //添加自定义短信登陆;
  78. http.authenticationProvider(getMyAuthenticationProvider());
  79. //验证路径
  80. authorizeConfigProviderManager.config(http.authorizeRequests());
  81. }
  82. private AuthenticationProvider getMyAuthenticationProvider(){
  83. UserNamePasswordAuthenticationProvider userNamePasswordAuthenticationProvider = new UserNamePasswordAuthenticationProvider();
  84. userNamePasswordAuthenticationProvider.setUserDetailsService(userServiceProvider);
  85. userNamePasswordAuthenticationProvider.setHideUserNotFoundExceptions(false);
  86. userNamePasswordAuthenticationProvider.setPasswordEncoder(passwordEncoder);
  87. return userNamePasswordAuthenticationProvider;
  88. }
  89. @Override
  90. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  91. resources.
  92. authenticationManager(authenticationManager)
  93. .tokenStore(redisTokenStore)
  94. .expressionHandler(oAuth2WebSecurityExpressionHandler);
  95. }
  96. /**
  97. * 解决bug
  98. * Failed to evaluate expression '#oauth2.throwOnError
  99. * No bean resolver registered in the context to resolve access to bean
  100. * @param applicationContext
  101. * @return
  102. */
  103. @Bean
  104. @Primary
  105. public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
  106. OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
  107. expressionHandler.setApplicationContext(applicationContext);
  108. return expressionHandler;
  109. }
  110. @Bean
  111. @ConditionalOnMissingClass
  112. public ReflectionSaltSource reflectionSaltSource() {
  113. ReflectionSaltSource reflectionSaltSource=new ReflectionSaltSource();
  114. reflectionSaltSource.setUserPropertyToUse("getSalt");
  115. return reflectionSaltSource;
  116. }
  117. @Inject
  118. public DaoAuthenticationProvider authenticationProvider() {
  119. DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
  120. provider.setHideUserNotFoundExceptions(false);
  121. provider.setUserDetailsService(userServiceProvider);
  122. provider.setPasswordEncoder(passwordEncoder);
  123. provider.setSaltSource(reflectionSaltSource());
  124. return provider;
  125. }
  126. // @Autowired
  127. // public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  128. //
  129. // auth.parentAuthenticationManager(new ProviderManager(new ArrayList<>(Arrays.asList(authenticationProvider()))));
  130. // }
  131. }