Spring Security简介
springSecurity是spring生产的一个安全框架, 也就是权限管理框架
作用:
将springSecurity集成到我们系统中, 我们就可以用它来完成系统的权限控制管理.
a) 用户名, 密码认证(判断用户名密码是否正确)
b) 授权: 给予登录用户对应的访问权限.
一、Spring Security Demo
引入jar包(略)
核心配置文件
1、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>
<!-- springSecurity 是基于Filter链的框架,下面是他的入口 -->
<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">
<!-- 页面拦截规则, use-expressions属性设置FALSE, 禁用spel表达式, 默认是true使用,
spel表达式: hasRole('ROLE_USER') -->
<http use-expressions="false">
<!-- 配置拦截路径: pattern: 设置拦截的路径/**指定拦截所有路径,
access:设置访问这个路径所具有的权限,
access权限写法: 必须以ROLE_开头, 后面跟大写字母 -->
<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>
即使没有编写登陆页面,系统会自动生成登陆页面
注意:(使用自带登陆页面时的默认项,如果自己编写登陆流程则不必遵守)
- 登录页面提交用户名密码表单路径必须叫做 /login
- 登录页面用户名和密码输入框中的name属性值必须叫做username和password
如果自定义登陆页面则需要修改配置文件 spring-security.xml
<!-- 以下页面不被拦截 -->
<!-- security="none" 设置此资源不被拦截 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/login_error.html" security="none"></http>
<!-- 页面拦截规则 -->
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_USER" />
<!--
login-page:指定登录页面。
authentication-failure-url:指定了身份验证失败时跳转到的页面。
default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。
-->
<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
<!-- 关闭csrf 允许跨域请求 -->
<csrf disabled="true"/>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
二、Spring Security 应用
概述
基于SOA架构实现web跨项目调用service中Spring Security 的应用
登陆流程
自定义页面 shoplogin.html
<form id="loginform" action="/login" method="post" class="sui-form">
<div class="input-prepend"><span class="add-on loginname"></span>
<input name="username" id="prependedInput" type="text" placeholder="邮箱/用户名/手机号" class="span2 input-xfat">
</div>
<div class="input-prepend"><span class="add-on loginpwd"></span>
<input name="password" id="prependedInput" type="password" placeholder="请输入密码" class="span2 input-xfat">
</div>
<div class="setting">
<label class="checkbox inline"><input name="m1" type="checkbox" value="2" checked="">自动登录</label>
<span class="forget">忘记密码?</span>
</div>
<div class="logined">
<a class="sui-btn btn-block btn-xlarge btn-danger" onclick="document:loginform.submit()" target="_blank">登 录</a>
</div>
</form>
web层项目中创建实现类 UserDetailsServiceImpl,实现 UserDetailsService接口
/**
* 自定义验证类, 实现springSecurity框架的UserDetailService接口
*/
public class UserDetailServiceImpl implements UserDetailsService {
private SellerService sellerService;
public void setSellerService(SellerService sellerService) {
this.sellerService = sellerService;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
authList.add(new SimpleGrantedAuthority("ROLE_SELLER"));
//1. 判断用户名是否为空, 如果为空直接返回null
if (username == null) {
return null;
}
//2. 根据用户名到数据库查询对应的用户对象
Seller seller = sellerService.findOne(username);
//3. 如果用户对象查不到, 返回null
if (seller != null) {
//4. 如果用户对象查到了, 判断是否已经审核通过, 如果未通过返回null
if ("1".equals(seller.getStatus())) {
//5. 返回springSecurity的User对象, 将这个用户的用户名, 密码, 所应该具有的访问权限集合返回
return new User(username, seller.getPassword(), authList);
}
}
return null;
}
}
web层项目中的spring目录下创建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"/>
<!-- 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">
<!--<user-service>-->
<!--<user name="admin" password="123456" authorities="ROLE_ADMIN"/>-->
<!--</user-service>-->
<!--<password-encoder ref="passwordEncoder"></password-encoder> -->
</authentication-provider>
</authentication-manager>
<!-- 引用dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.200.128:2181"/>
<dubbo:reference id="sellerService" interface="cn.itcast.core.service.SellerService" >
</dubbo:reference>
<!-- 配置自定义的认证类 -->
<beans:bean id="userDetailService" class="cn.itcast.core.service.UserDetailServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean>
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans:beans>
注意:
- springSecurity如果需要去数据库中找对应的用户对象进行验证, 就需要自定义UserDetailService实现类
- 因为在自定义的service中需要使用其他的service到数据库中查询, 所以需要注入, 而这里sellerService是在service-sellerGoods项目中, 跨项目调用需要使用dubbo注入.
- 在springMvc.xml中虽然配置了包扫描但是扫描的是controller包, 只有在这个包下面写@Refrence注解才会生效
- 在我们UserDetailService实现类中无法使用dubbo注解进行注入, 所以我们这里利用xml配置文件的标签<dubbo:refrence 进行注入sellerService(此时只是注入进项目中spring工厂)
- 在userDetailService中如果使用@Service注解无法指定注入的属性或者对象, 所以这里我们使用最原始的set方法注入, 可以在配置文件中配置bean, 在bean标签了配置<property标签注入SellerService属性
(因为userDetailService需要交给spring管理,使用注解@Service则sellerService不能注入进userDetailService中(因为它不能使用注解注入),所以只能使用原始配置文件bean注入)