SpringSecurityConfig.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.yihu.wlyy.config;
  2. import com.yihu.wlyy.filter.AccessDecisionManagerImpl;
  3. import com.yihu.wlyy.filter.AccessDeniedHandlerImpl;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.security.access.AccessDecisionManager;
  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.EnableWebSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  11. import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
  12. /**
  13. * @author lincl
  14. * @version 1.0
  15. * @created 2016/7/21
  16. */
  17. @EnableWebSecurity
  18. public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  19. @Override
  20. public void configure(WebSecurity web) throws Exception {
  21. // 设置不拦截规则
  22. web.ignoring().antMatchers(
  23. "/static/**",
  24. "/error/**",
  25. "/out",
  26. "/**.jsp",
  27. "/common/**",
  28. "/login/**",
  29. "/yueren/**",
  30. "/customer/**",
  31. "/third/**",
  32. "/admin/hos/doctor/importFromExcel",
  33. "/admin/hos/doctor/importData",
  34. "/admin/team/importData",
  35. "/admin/hos/importData",
  36. "/admin/hos/doctor/toExcel",
  37. "/admin/device/toExcel",
  38. "/admin/patientDevice/toExcel",
  39. "/admin/healthIndex/toExcel",
  40. "/admin/static/prescription/toExcel",
  41. "/admin/static/wechat/listToExcel",
  42. "/admin/static/wechat/hosipitaTotalToExcel",
  43. "/admin/static/wechat/townTotalToExcel",
  44. "/admin/basedata/importData",
  45. "/admin/wlyyUserRole/importData",
  46. "/WEB—INF/views/**"
  47. );
  48. }
  49. @Override
  50. protected void configure(HttpSecurity http) throws Exception {
  51. // 设置拦截规则
  52. http
  53. .authorizeRequests()
  54. .accessDecisionManager(accessDecisionManager())
  55. .expressionHandler(webSecurityExpressionHandler())
  56. .antMatchers("/yueren/**").permitAll()
  57. // .antMatchers("/admin/main").permitAll()
  58. // .antMatchers("/login/**").permitAll()
  59. // .antMatchers("/admin/**").authenticated()
  60. .antMatchers("/admin/**").hasRole("USER")
  61. .and()
  62. .exceptionHandling().accessDeniedHandler(accessDeniedHandler())
  63. .and()
  64. // .headers().frameOptions().disable();//取消jfame的安全验证
  65. // .and()
  66. .headers().disable();
  67. // 自定义登录页面
  68. // http.csrf().disable().formLogin().loginPage("/login").permitAll();
  69. // 自定义注销
  70. // http.logout().logoutUrl("/logout").logoutSuccessUrl("/login")
  71. // .invalidateHttpSession(true);
  72. // session管理
  73. // http.sessionManagement().sessionFixation().changeSessionId()
  74. // .maximumSessions(1).expiredUrl("/");
  75. // RemeberMe 和UserDetailsService合作 用来保存用户信息, 一段时间内可以不用在输入用户名和密码登录,暂不使用该功能
  76. // http.rememberMe().key("webmvc#FD637E6D9C0F1A5A67082AF56CE32485");
  77. }
  78. @Override
  79. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  80. }
  81. /*
  82. * 最终访问控制器
  83. */
  84. @Bean(name = "accessDecisionManager")
  85. public AccessDecisionManager accessDecisionManager() {
  86. return new AccessDecisionManagerImpl();
  87. }
  88. /*
  89. * 错误信息拦截器
  90. */
  91. @Bean(name = "accessDeniedHandler")
  92. public AccessDeniedHandlerImpl accessDeniedHandler() {
  93. AccessDeniedHandlerImpl accessDeniedHandler = new AccessDeniedHandlerImpl();
  94. // accessDeniedHandler.setErrorPage("/error/403.jsp");
  95. return accessDeniedHandler;
  96. }
  97. /*
  98. * 表达式控制器
  99. */
  100. @Bean(name = "expressionHandler")
  101. public DefaultWebSecurityExpressionHandler webSecurityExpressionHandler() {
  102. DefaultWebSecurityExpressionHandler webSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
  103. return webSecurityExpressionHandler;
  104. }
  105. }