ResourceServerConfig.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.yihu.base.security;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.authentication.AuthenticationManager;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  7. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  8. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  9. import org.springframework.security.oauth2.provider.token.TokenStore;
  10. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  11. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  12. /**
  13. * Created by chenweida on 2017/12/4.
  14. */
  15. @Configuration
  16. @EnableResourceServer //开启资源服务器
  17. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  18. @Autowired
  19. protected AuthenticationSuccessHandler authenticationSuccessHandler;
  20. @Autowired
  21. protected AuthenticationFailureHandler authenticationFailureHandler;
  22. @Autowired
  23. private AuthenticationManager authenticationManager;
  24. @Autowired
  25. private TokenStore redisTokenStore;
  26. @Override
  27. public void configure(HttpSecurity http) throws Exception {
  28. //这是账号密码登陆
  29. http.formLogin()//设置验证码 账号密码登陆
  30. .loginPage("/denglu.html")
  31. .loginProcessingUrl("/authentication/form")
  32. .successHandler(authenticationSuccessHandler)
  33. .failureHandler(authenticationFailureHandler)
  34. .and()
  35. .authorizeRequests()
  36. .antMatchers(
  37. "/denglu.html",
  38. "/authentication/form").permitAll()
  39. .anyRequest().authenticated()
  40. .and()
  41. .csrf().disable();
  42. }
  43. @Override
  44. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  45. resources.
  46. authenticationManager(authenticationManager).
  47. tokenStore(redisTokenStore);
  48. }
  49. }