1.SpringSecurity入门
spring security 是一个强大高度定制化的集认证与权限控制于一身的框架,主要关注与给java应用提供认证与授权,由于它是spring家族的,你会发现在几乎所有spring项目中,他能轻松的扩展到满足各种定制化需求
它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作securi
2.SpringSecurity小Demo演示
(1)创建工程spring-security-demo ,pom.xml内容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.itcast.demo</groupId> <artifactId>spring-security-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <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>9090</port> <!-- 请求路径 --> <path>/</path> </configuration> </plugin> </plugins> </build> </project> |
(2)创建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> |
(3)创建index.html 内容略
(4)创建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" 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> |
此案例我们没有登录页,而是使用了系统自动生成的登陆页,效果如下:
配置说明:
intercept-url 表示拦截页面
/* 表示的是该目录下的资源,只包括本级目录不包括下级目录
/** 表示的是该目录以及该目录下所有级别子目录的资源
form-login 为开启表单登陆
use-expressions 为是否使用使用 Spring 表达式语言( SpEL ),默认为true ,如果开启,则拦截的配置应该写成以下形式
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> |
自己定义登录页的话,把表单里配置默认aciton="/login",name='username',name=‘password’
然后更改配置如下
<!-- 以下页面不被拦截 --> <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" /> <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/> <csrf disabled="true"/> </http> |
解释:因为访问任何页面都要先登录,那你访问登录页也要登录岂不是死循环了,所以security="none" 设置此资源不被拦截.
login-page:指定登录页面。
authentication-failure-url:指定了身份验证失败时跳转到的页面。
default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。
csrf disabled="true" 关闭csrf ,如果不加会出现错误(如果整站都是html就设置)
3.运营商登录实现
1.pom文件里新增这两个依赖即可,其它spring依赖都有
2.spring-security.xml里静态资源文件与登录页面错误页面等设置为不拦截
<http pattern="/login.html" security="none"></http>
<http pattern="/css/**" security="none"></http>
<http pattern="/img/**" security="none"></http>
<http pattern="/js/**" security="none"></http>
<http pattern="/plugins/**" security="none"></http>
然后由于是框架页形式,要加以下配置
然后在用户少的情况下可手动设置用户名密码,后面教会从数据库查
3.web.xml里高亮部分根据spring-security.xml实际路径配置
4.登录页面还是老套路,表单提交方式必须写post,加个 id = 'loginform'方便后面添加事件
然后给登录按钮添加事件,
4.显示当前登录用户名
前端还是老套路,引入模块,controller调service,service里调后台,绑定变量
<body class="hold-transition skin-green sidebar-mini" ng-app="pinyougou" ng-controller="indexController" ng-init="showLoginName ()">
后台获取当前登录人要重点提下(得到当前上下文对象再得到认证对象的名称) service调后台的时候注意目录结构
@RestController
@RequestMapping("/login")
public class LoginController {
@RequestMapping("name")
public Map name(){
String name=SecurityContextHolder.getContext()
.getAuthentication().getName();
Map map=new HashMap();
map.put("loginName", name);
return map ;
}
}
5.注销
在pinyougou-manager-web的spring-security.xml的http节点中添加配置
<logout/> |
加此配置后,会自动的产生退出登录的地址/logout,如果你不想用这个地址 ,你也可以定义生成的退出地址以及跳转的页面,配置如下
<logout logout-url="" logout-success-url=""/> |
logout-url:退出的地址,会自动生成
logout-success-url:退出后跳转的地址
修改注销的链接
<div class="pull-right"> <a href="../logout" class="btn btn-default btn-flat">注销</a> </div> |
6.商家入驻申请
每一模块记得新建一个controller与service
前端绑定seller实体,然后绑定add方法,后台关键代码如下
7.商家审核
状态值: 0:未审核 1:已审核 2:审核未通过 3:关闭
前端老套路绑定实体等,然后添加事件
sellerService.js里代码
this.updateStatus=function(sellerId,status){
return $http.get('../seller/updateStatus.do?sellerId='+sellerId+'&status='+status);
}
后台关键代码
public void updateStatus(String sellerId, String status) {
TbSeller seller = sellerMapper.selectByPrimaryKey(sellerId);
seller.setStatus(status);
sellerMapper.updateByPrimaryKey(seller);
}
8.商家系统登录与安全控制
首先了解下加密
常用加密分为两种
1.秘钥加密 可根据秘钥反向推导出原密文
2.算法加密 不可推导,但是由于每个原密文加密后得到固定的加密密文,所以一些在线破解的网站利用大数据海量存储(比如我把123456,888888等简易的都md5加密后放到数据库,破解其实就是到数据库中查,),可以破解一些简单的md5加密密文
这里我们将用的是BCript,比如同是123456,存到数据库两次结果不同,就是先经过md5加密然后随机产生盐然后混合进去,想了解详情可自己去深入学习
实现:
//密码加密 获取加密对象 调用encode加密方法 这里我已从controller得到了申请入驻商家的的seller对象
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String password = passwordEncoder.encode(seller.getPassword());
seller.setPassword(password);
上面是完善商家信息的安全性
下面将以登录过程讲讲springSecurity的使用
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="/js/***" security="none"/>
<http pattern="/img/**" security="none"/>
<http pattern="/plugins/**" security="none"/>
<http pattern="/seller/add.do" security="none"></http>
<!-- 是否启用SPEL表达式,SPEL表达式虽然功能多但书写麻烦 -->
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_SELLER" />
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html"
authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
<csrf disabled="true"/>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<logout/>
</http>
<!-- 配置加密方式,登录时springsecurity帮你对比-->
<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
<password-encoder ref="bCryptEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<!-- 配置认证类的父接口-->
<beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailsServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean>
<!-- 引用dubbo 服务 需要跨项目调方法时 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.168:2181"/>
<dubbo:reference id="sellerService" interface="com.pinyougou.sellergoods.service.SellerService"></dubbo:reference>
<!--配置Bcrypt加密类-->
<beans:bean id="bCryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"> </beans:bean>
</beans:beans>
都配置好后实现认证接口,然后判断若该用户名密码匹配数据库里的并且状态为审核通过,则跳转主页,否则打回登录页面