赞
踩
spring Security是一个能够为基于spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在spring应用上下文中配置的bean,充分利用了springioc(控制反转),di(依赖注入)和aop(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制 编写大量重复代码的工作。
入门小demo:
pom.xml:
- <properties>
- <spring.version>4.2.4.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-web</artifactId>
- <version>4.1.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-config</artifactId>
- <version>4.1.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <!-- java编译插件 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.tomcat.maven</groupId>
- <artifactId>tomcat7-maven-plugin</artifactId>
- <configuration>
- <!-- 指定端口 -->
- <port>8083</port>
- <!-- 请求路径 -->
- <path>/</path>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-security.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <filter>
- <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
创建spring-security.xml配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
-
- <!-- 页面拦截规则 -->
- <http use-expressions="false">
- <intercept-url pattern="/**" access="ROLE_USER" />
- <form-login/>
- </http>
-
- <!-- 认证管理器 -->
- <authentication-manager>
- <authentication-provider>
- <user-service>
- <user name="admin" password="123456" authorities="ROLE_USER"/>
- </user-service>
- </authentication-provider>
- </authentication-manager>
- </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配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans
- xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
-
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
-
- <http pattern="/*.html" security="none"/>
- <http pattern="/css/**" security="none"/>
- <http pattern="/img/**" security="none"/>
- <http pattern="/js/**" security="none"/>
- <http pattern="/plugins/**" security="none"/>
- <http pattern="/seller/add.do" security="none"/>
-
- <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
- <http use-expressions="false">
- <!--
- 配置SpringSecurity的拦截路径(拦截规则)
- * pattern:配置拦截规则。 /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
- * access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
- -->
- <intercept-url pattern="/**" access="ROLE_SELLER"/>
-
- <!--
- 开启表单验证
- username-parameter="username"
- password-parameter="password"
- login-page :登录页面名称 以 / 开始
- default-target-url :登录成功后跳转的页面
- login-processing-url:提交的路径的设置 默认值"/login" 可以修改
- -->
- <form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/>
-
- <!-- 不使用csrf的校验 -->
- <csrf disabled="true"/>
-
- <!-- 配置框架页面不拦截 -->
- <headers>
- <frame-options policy="SAMEORIGIN"/>
- </headers>
- <!-- 注销的配置 -->
- <logout logout-url="/logout" logout-success-url="/shoplogin.html" />
-
- </http>
-
- <!-- 配置认证管理器 -->
- <authentication-manager>
- <!-- 认证的提供者 -->
- <authentication-provider user-service-ref="userDetailService">
- <password-encoder ref="passwordEncoder"></password-encoder>
- </authentication-provider>
- </authentication-manager>
-
- <!-- 引用dubbo 服务消息 方 -->
- <dubbo:application name="pinyougou-web-shop" />
- <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
- <dubbo:reference id="sellerService" interface="cn.it.core.service.SellerService" />
-
- <!-- 配置自定义的认证类 -->
- <beans:bean id="userDetailService" class="cn.it.core.service.UserDetailServiceImpl">
- <beans:property name="sellerService" ref="sellerService"/>
- </beans:bean>
- <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
- </beans:beans>
在SellerService中写接口:
public Seller findSellerBySellerIdAndStatus(String sellerId, String status);
UserDetailServiceImpl类:
- public class UserDetailServiceImpl implements UserDetailsService {
-
- // 根据用户名查询tb_seller 表
- private SellerService sellerService;
-
- public void setSellerService(SellerService sellerService) {
- this.sellerService = sellerService;
- }
-
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- // TODO Auto-generated method stub
- // 认证
- Seller seller = sellerService.findSellerBySellerIdAndStatus(username, "1");
- if (null != seller) {
- Set<GrantedAuthority> authorities = new HashSet<>();
- authorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
- return new User(seller.getSellerId(), seller.getPassword(), authorities);
-
- } else {
- return null;
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。