当前位置:   article > 正文

java安全框架——Spring Security安全框架_spring安全框架

spring安全框架

spring Security是一个能够为基于spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在spring应用上下文中配置的bean,充分利用了springioc(控制反转),di(依赖注入)和aop(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制 编写大量重复代码的工作。

入门小demo:

pom.xml:

  1. <properties>
  2. <spring.version>4.2.4.RELEASE</spring.version>
  3. </properties>
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-core</artifactId>
  8. <version>${spring.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-web</artifactId>
  13. <version>${spring.version}</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-webmvc</artifactId>
  18. <version>${spring.version}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context-support</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-test</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-jdbc</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.security</groupId>
  37. <artifactId>spring-security-web</artifactId>
  38. <version>4.1.0.RELEASE</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.security</groupId>
  42. <artifactId>spring-security-config</artifactId>
  43. <version>4.1.0.RELEASE</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>javax.servlet</groupId>
  47. <artifactId>servlet-api</artifactId>
  48. <version>2.5</version>
  49. <scope>provided</scope>
  50. </dependency>
  51. </dependencies>
  52. <build>
  53. <plugins>
  54. <!-- java编译插件 -->
  55. <plugin>
  56. <groupId>org.apache.maven.plugins</groupId>
  57. <artifactId>maven-compiler-plugin</artifactId>
  58. <version>3.2</version>
  59. <configuration>
  60. <source>1.7</source>
  61. <target>1.7</target>
  62. <encoding>UTF-8</encoding>
  63. </configuration>
  64. </plugin>
  65. <plugin>
  66. <groupId>org.apache.tomcat.maven</groupId>
  67. <artifactId>tomcat7-maven-plugin</artifactId>
  68. <configuration>
  69. <!-- 指定端口 -->
  70. <port>8083</port>
  71. <!-- 请求路径 -->
  72. <path>/</path>
  73. </configuration>
  74. </plugin>
  75. </plugins>
  76. </build>
  77. </project>

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version="2.5">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:spring-security.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>
  12. org.springframework.web.context.ContextLoaderListener
  13. </listener-class>
  14. </listener>
  15. <filter>
  16. <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>springSecurityFilterChain</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. </web-app>

创建spring-security.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"
  3. xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
  6. <!-- 页面拦截规则 -->
  7. <http use-expressions="false">
  8. <intercept-url pattern="/**" access="ROLE_USER" />
  9. <form-login/>
  10. </http>
  11. <!-- 认证管理器 -->
  12. <authentication-manager>
  13. <authentication-provider>
  14. <user-service>
  15. <user name="admin" password="123456" authorities="ROLE_USER"/>
  16. </user-service>
  17. </authentication-provider>
  18. </authentication-manager>
  19. </beans:beans>

在web.xml中配置了filter拦截器,安全框架拦截所有请求,拦截器需要有配置文件,配置文件是需要监听器来加载的,

spring-security.xml配置文件里面就是拦截的配置。

配置不被拦截的页面:

<http pattern="/login.html" security="none"></http>

需要控制安全的放在<http>标签中:

<http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER" /> :pattern="/**" 拦截所有,表示有ROLE_USER权限可以访问
        <form-login default-target-url="/index.html" always-use-default-target="true"
            login-page="/login.html"
  login-processing-url="/login"/> : login-page表示自己的登录页, login-processing-url登录页处理的请求,在自己书写的登录页面中form表单的action路径必须也要是/login。登录成功跳转到/index.html页面,可以配置登录失败返回登录页面重新
          
        <!-- csrf disabled="true"  关闭csrf(跨域请求攻击,被攻击了是因为cookie被挟持了),如果不加会出现错误 cookie (走网线)+手机短信 (4G) -->
        <csrf disabled="true"/>
    </http>

认证器的任务就是判断用户名,密码好角色是否正确,如果正确认证成功。实际这些信息时存放在表里,这里只是做一个小demo,先暂时写死。

 

实际应用:(我们给系统中添加)

1.首先将上方的web.xml中的内容,放到项目中的web.xml中。

2.将spring-security.xml配置文件放到项目中(注意加载路径正确)。

3.修改spring-security.xml配置文件内容:

01.项目中spring-security.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans
  3. xmlns="http://www.springframework.org/schema/security"
  4. xmlns:beans="http://www.springframework.org/schema/beans"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
  10. <http pattern="/*.html" security="none"/>
  11. <http pattern="/css/**" security="none"/>
  12. <http pattern="/img/**" security="none"/>
  13. <http pattern="/js/**" security="none"/>
  14. <http pattern="/plugins/**" security="none"/>
  15. <http pattern="/seller/add.do" security="none"/>
  16. <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
  17. <http use-expressions="false">
  18. <!--
  19. 配置SpringSecurity的拦截路径(拦截规则)
  20. * pattern:配置拦截规则。 /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
  21. * access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
  22. -->
  23. <intercept-url pattern="/**" access="ROLE_SELLER"/>
  24. <!--
  25. 开启表单验证
  26. username-parameter="username"
  27. password-parameter="password"
  28. login-page :登录页面名称 以 / 开始
  29. default-target-url :登录成功后跳转的页面
  30. login-processing-url:提交的路径的设置 默认值"/login" 可以修改
  31. -->
  32. <form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/>
  33. <!-- 不使用csrf的校验 -->
  34. <csrf disabled="true"/>
  35. <!-- 配置框架页面不拦截 -->
  36. <headers>
  37. <frame-options policy="SAMEORIGIN"/>
  38. </headers>
  39. <!-- 注销的配置 -->
  40. <logout logout-url="/logout" logout-success-url="/shoplogin.html" />
  41. </http>
  42. <!-- 配置认证管理器 -->
  43. <authentication-manager>
  44. <!-- 认证的提供者 -->
  45. <authentication-provider user-service-ref="userDetailService">
  46. <password-encoder ref="passwordEncoder"></password-encoder>
  47. </authentication-provider>
  48. </authentication-manager>
  49. <!-- 引用dubbo 服务消息 方 -->
  50. <dubbo:application name="pinyougou-web-shop" />
  51. <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
  52. <dubbo:reference id="sellerService" interface="cn.it.core.service.SellerService" />
  53. <!-- 配置自定义的认证类 -->
  54. <beans:bean id="userDetailService" class="cn.it.core.service.UserDetailServiceImpl">
  55. <beans:property name="sellerService" ref="sellerService"/>
  56. </beans:bean>
  57. <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
  58. </beans:beans>

在SellerService中写接口:

public Seller findSellerBySellerIdAndStatus(String sellerId, String status);

UserDetailServiceImpl类:

  1. public class UserDetailServiceImpl implements UserDetailsService {
  2. // 根据用户名查询tb_seller 表
  3. private SellerService sellerService;
  4. public void setSellerService(SellerService sellerService) {
  5. this.sellerService = sellerService;
  6. }
  7. @Override
  8. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  9. // TODO Auto-generated method stub
  10. // 认证
  11. Seller seller = sellerService.findSellerBySellerIdAndStatus(username, "1");
  12. if (null != seller) {
  13. Set<GrantedAuthority> authorities = new HashSet<>();
  14. authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
  15. return new User(seller.getSellerId(), seller.getPassword(), authorities);
  16. } else {
  17. return null;
  18. }
  19. }
  20. }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/636140
推荐阅读
相关标签
  

闽ICP备14008679号