SpringSecurity的简单使用
导入SpringSecurity坐标
在web.xml中配置过滤器
编写spring-securiy配置文件
编写自定义认证提供者
用户新增时加密密码
配置页面的login和logout
获取登录用户的信息
一.SpringSecurity简介
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
如果要对Web资源进行保护,最好的办法莫过于Filter,要想对方法调用进行保护,最好的办法莫过于AOP。Spring security对Web资源的保护,就是靠Filter实现的。
二.SpringSecurity的使用
1.导入SpringSecurity的坐标
<!-- SpringSecurity相关坐标 -->
<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>
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">
<!-- 1.解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 2.配置SpringMVC的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 3.配置SpringSecurity的过滤器(以一当十) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/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.编写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:dubbo="http://code.alibabatech.com/schema/dubbo"
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://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"> <!-- 1.配置页面的放行规则(不需要登录验证的资源) -->
<http pattern="/*.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>
<http pattern="/seller/add.do" security="none"></http> <!-- 2.页面的拦截规则 -->
<http use-expressions="false">
<!-- 2.1当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
<intercept-url pattern="/**" access="ROLE_USER"/>
<!-- 2.2表单登陆,默认用户名和密码的name属性为:username和password,也可在这里配置 -->
<form-login login-page="/shoplogin.html"
default-target-url="/admin/index.html"
authentication-failure-url="/shoplogin.html"
always-use-default-target="true"/>
<!-- 2.3关闭跨域攻击 -->
<csrf disabled="true"/> <!-- 2.4为了解决frame框架访问问题默认是deny不允许访问,改成同一域下可以进行访问-->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<!-- 2.5配置登出功能(页面注销连接到“/logout"即可完成退出到指定页面) -->
<logout logout-success-url="/login.html"></logout>
</http> <!-- 3.认证管理器 -->
<authentication-manager>
<!-- 3.1认证提供者:这里是写成固定的,也可以自定义 -->
<authentication-provider>
<user-service>
<!-- 配置当前系统的用户 authorities该用户属于哪个角色:这里写成固定的 -->
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
<!-- 3.1认证提供者:这里是写成固定的,也可以自定义 -->
<!-- ======================================================== -->
<!-- 3.2通过自定义认证提供者,实现动态认证 -->
<authentication-provider user-service-ref="userDetailService">
<!-- 认证时,先对用户输入的密码加密再和数据库的对比 -->
<password-encoder ref="bcryptEncoder"></password-encoder>
</authentication-provider>
<!-- 3.2通过自定义认证提供者,实现动态认证 -->
</authentication-manager> <!-- 4.认证类:配置的方式进行注入 -->
<beans:bean id="userDetailService" class="cn.dintalk.service.UserDetailsServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean> <!-- 5.引用dubbo 服务 -->
<dubbo:application name="dintalk-shop-web" />
<dubbo:registry address="zookeeper://192.168.88.130:2181"/>
<!-- 5.1配置的方式注入sellerService -->
<dubbo:reference id="sellerService" interface=
"cn.dintalk.sellergoods.service.SellerService"></dubbo:reference> <!-- 6.配置密码加密方式 -->
<beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
</beans:beans>
4.编写自定义认证提供者(如需自定义)
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
/**
* 用户的登录认证
* @author Mr.song
* @date 2019/06/06 12:26
*/
public class UserDetailsServiceImpl implements UserDetailsService { /**
* 提供set方法以注入sellerService
*/
private SellerService sellerService;
public void setSellerService(SellerService sellerService) {
this.sellerService = sellerService;
} @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.根据用户名查询数据库
TbSeller seller = sellerService.findOne(username);
//2.判断用户是否存在
if (seller != null){
//3.定义集合,封装用户的角色(这里角色少,写死.也可以从数据库查询)
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
if (seller.getStatus().equals("1")){//用户处于可以登录的状态
return new User(username,seller.getPassword(),grantedAuthorities);
}
}
//3.用户不存在,认证失败
return null;
}
}
5.用户新增时加密密码(如需加密)
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* 增加
* @param seller
* @return
*/
@RequestMapping("/add")
public Result add(@RequestBody TbSeller seller){
try {
//添加时进行密码的加密,登录时配置同样的加密器即可
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String newPws = passwordEncoder.encode(seller.getPassword());
seller.setPassword(newPws);
sellerService.add(seller);
return new Result(true, "增加成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "增加失败");
}
}
6.配置页面的login和logout
<!-- 1.login的配置要点:默认,登录框的name属性分别为username和password(也可在配置中修改)
登录表单提交方式为post,登录链接为:/login-->
<form method="post" id="loginform" action="/login">
<input name="username" type="text" placeholder="邮箱/用户名/手机号">
<input name="password" type="password" placeholder="请输入密码">
<a onclick="document:loginform.submit()" target="_blank">登 录</a>
</form> <!-- 2.logout的配置要点:默认,退出链接为:/logout即可 -->
<a href="/logout" >注销</a>
7.获取登录用户的信息
import org.springframework.security.core.context.SecurityContextHolder;
...
/**
* 获取用户登录名进行展示
* @return
*/
@RequestMapping("/showName")
public Map showName(){
//1.从认证处取得登录信息(除了username还可获取其他信息)
String name = SecurityContextHolder.getContext().getAuthentication().getName();
//2.构建Map并返回
HashMap<String, String> map = new HashMap<>();
map.put("name",name);
return map;
}
关注微信公众号,随时随地学习

SpringSecurity的简单使用的更多相关文章
- SpringSecurity的简单应用(一)
java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downlo ...
- SpringSecurity配置,简单梳理
生活加油:摘一句子: “我希望自己能写这样的诗.我希望自己也是一颗星星.如果我会发光,就不必害怕黑暗.如果我自己是那么美好,那么一切恐惧就可以烟消云散.于是我开始存下了一点希望—如果我能做到,那么我就 ...
- SpringSecurity:简单入门
SpringSecurity能做什么 SpringSecurity是一个安全框架,使用它可以让我们的系统变得安全一点,它可以对登陆系统的用户进行验证和授权 一个安全的系统需要做的事情很多,比如:防SQ ...
- SpringSecurity的简单入门
以下是大体思路 1.导入坐标 <properties> <spring.version>4.2.4.RELEASE</spring.version> </pr ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- web项目学习之spring-security
转自<http://liukai.iteye.com/blog/982088> spring security功能点总结: 1. 登录控制 2. 权限控制(用户菜单的显示,功能点访问控制) ...
- springSecurity自定义认证配置
上一篇讲了springSecurity的简单入门的小demo,认证用户是在xml中写死的.今天来说一下自定义认证,读取数据库来实现认证.当然,也是非常简单的,因为仅仅是读取数据库,权限是写死的,因为相 ...
- spring-boot-learning-spring Security
SpringSecurity的简单原理: 一旦启用了Spring Security,Spring IoC容器就会为你创建一个名称为springSecurityFilterChain 的Spring B ...
- Java-Springboot-集成spring-security简单示例(Version-springboot-2-1-3-RELEASE
使用Idea的Spring Initializr或者SpringBoot官网下载quickstart 添加依赖 1234 <dependency><groupId>org.sp ...
随机推荐
- SonarQube---在具体项目中的使用
一.简介 SonarQube(简称Sonar)是管理代码质量的开放平台,它可以快速地对代码质量进行分析,并给出合理的解决方案,提高管理效率,保证代码质量. Sonar官网,文档 Sonar Scann ...
- react 中的 PureComponent
React.PureComponent最重要的一个用处就是优化React应用,因为它减少了应用中的渲染次数,所以对性能的提升是非常可观的. 原理:在普通的 componnet 组件中,shouldCo ...
- Xcode The identity used to sign the executable is no longer valid. 错误解决
Xcode真机调试时出现问题:Xcode The identity used to sign the executable is no longer valid. Please verify that ...
- Ios 项目从头开发 MVVM模式(三)
1.话说,本来想做个聚合查询功能.可是我的重点想研究xmpp聊天功能.所以使用mvvm模式做了全然模式51job主界面的页面. 2.首先给大家看我执行起来的界面. 3.界面非常easy,做这个界面主要 ...
- java8--IO(java疯狂讲义3复习笔记)
产生文件 File file = new File("abc.txt"); if(!file.exists()){ System.out.println(file.exists() ...
- 《31天成为IT服务达人》--做事篇(第四章)之如何找目标
前面介绍了什么是IT服务.以下几章将介绍IT服务该怎么做.在聊怎么做之前.想起几句流行的告白和准备入行IT服务事业的朋友共勉. 当你的才华 还撑不起你的野心时 就应该静下心来 学习 --- 当你 ...
- [MAC] Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead.
MAC启动GoAgent,出现Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead. 解决方法:为Python安装pycrypto,可通过Py ...
- Buildroot构建指南——根文件系统(Rootfs)【转】
本文转载自; 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Buildroot构建指南——根文件系统(Rootfs) Buildroot的Rootfs构建流程有一个大 ...
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- 实现的是Linux和Windows之间的一种共享--samba
samba 基本配置及自定义控制 https://www.cnblogs.com/l-hh/p/9473937.html Samba简介: Samba实现的是Linux和Windows之间的一种共享, ...