SecurityConfig.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.yihu.jw.config;
  2. import com.yihu.jw.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  10. import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
  11. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  12. import org.springframework.security.crypto.password.PasswordEncoder;
  13. /**
  14. * Created by chenweida on 2017/11/29.
  15. */
  16. @EnableWebMvcSecurity
  17. @Configuration
  18. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  19. @Autowired
  20. private UserService userService;
  21. @Autowired
  22. private BaseAuthenticationSuccessHandler baseAuthenticationSuccessHandler;
  23. @Autowired
  24. private BaseAuthenticationFailureHandler baseAuthenticationFailureHandler;
  25. /**
  26. * 处理用户密码加密解密
  27. * 密码加密工具类 验证密码使用 項目中使用要根據自己項目中的加密規則自定義
  28. *
  29. * @return
  30. */
  31. @Bean
  32. PasswordEncoder passwordEncoder() {
  33. return new BCryptPasswordEncoder();
  34. }
  35. /**
  36. * HttpSecurity:一般用它来具体控制权限,角色,url等安全的东西。
  37. *
  38. * @param http
  39. * @throws Exception
  40. */
  41. @Override
  42. protected void configure(HttpSecurity http) throws Exception {
  43. http
  44. .formLogin()
  45. .loginPage("/denglu.html") //自定义登陆页面
  46. .loginProcessingUrl("/authentication/form") //登陆页面的请求路径
  47. .usernameParameter("username") //登陆页面的usernma
  48. .passwordParameter("password") //登陆页面的password
  49. .successHandler(baseAuthenticationSuccessHandler) //认证成功之后的处理
  50. .failureHandler(baseAuthenticationFailureHandler) //认证失败之后的处理
  51. .and()
  52. .authorizeRequests()
  53. .antMatchers("/denglu.html", "/authentication/form").permitAll() ///denglu.html 不用认证
  54. .anyRequest().authenticated() //其他请求需要验证
  55. .and()
  56. .sessionManagement() //session 管理器
  57. .and()
  58. .userDetailsService(userService) //自定义用户认证
  59. .csrf().disable(); //关闭csrf (防止跨站请求仿造攻击)默认是开启的
  60. }
  61. /**
  62. * :用来做登录认证的
  63. *
  64. * @param auth
  65. * @throws Exception
  66. */
  67. @Override
  68. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  69. auth.jdbcAuthentication();
  70. }
  71. /**
  72. * For example, if you wish to ignore certain requests
  73. *
  74. * @param web
  75. * @throws Exception
  76. */
  77. @Override
  78. public void configure(WebSecurity web) throws Exception {
  79. super.configure(web);
  80. }
  81. }