spring-security4.1.2的学习
-
spring security教程
spring security是什么?
spring security所需jar包
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
spring security在web.xml中的配置
<!-- Spring Secutiry4.1的过滤器链配置 -->
<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>
spring security的配置文件内容如下
<?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">
<debug />
<http security="none" pattern="/login.jsp" />
<http security="none" pattern="/static/**" />
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<!-- 同一时间内允许同一账号保持4个在线,error-if-maximum-exceeded="true"表示第第四个以后的登不进去 -->
<session-management>
<concurrency-control max-sessions="4"
error-if-maximum-exceeded="true" />
</session-management>
<csrf disabled="true"/>
<form-login login-page="/login.jsp"
authentication-failure-handler-ref="authenticationFailureHandlerImpl"
authentication-success-handler-ref="authenticationSuccessHandlerImpl" />
<logout logout-success-url="/logout.jsp" logout-url="logout"
invalidate-session="true" delete-cookies="JSESSIONID" />
</http>
<authentication-manager>
<!-- <authentication-provider> -->
<!-- <user-service> -->
<!-- <user name="admin" password="123" authorities="ROLE_USER"/> -->
<!-- </user-service> -->
<!-- </authentication-provider> -->
<authentication-provider user-service-ref="userService">
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
<beans:bean id="userService" class="com.**.user.service.impl.UserServiceImpl" />
<!-- 认证成功调用 主要实现AuthenticationSuccessHandler这个类的onAuthenticationSuccess方法-->
<beans:bean id="authenticationSuccessHandlerImpl"
class="com.**.utils.springsecurity.AuthenticationSuccessHandlerImpl">
<beans:property name="url" value="/welcome.jsp" />
</beans:bean>
<!-- 认证失败调用 主要实现AuthenticationFailureHandler类的onAuthenticationFailure-- >
<beans:bean id="authenticationFailureHandlerImpl"
class="com.**.utils.springsecurity.AuthenticationFailureHandlerImpl">
<beans:property name="errorUrl" value="/error.jsp" />
</beans:bean>
</beans:beans>
com.**.user.service.impl.UserServiceImp.java
public class UserServiceImpl implements UserDetailsService{
@Autowired
private UserDao userDao;
public UserDetails loadUserByUsername(String username) {
UserDetails details = null;
try {
// 用户名,密码,是否激活,accountnonexpired如果帐户没有过期设置为true
// credentialsnonexpired如果证书没有过期设置为true
// accountnonlocked如果帐户不锁定设置为true
com.aoyu.user.entity.User u = this.getUser(username);
//目前是把角色给写死了
details = new org.springframework.security.core.userdetails.User(u.getUsername(), u.getPassword(), u.isEnabled(),u.isAccountNonExpired(),u.isCredentialsNonExpired(),u.isAccountNonLocked(),AuthorityUtils.createAuthorityList("ROLE_USER"));
} catch (UsernameNotFoundException usernameNotFoundException) {
usernameNotFoundException.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return details;
}
}
tips:你的只有自己动手敲代码,你才可也学得更快,最后分享几个spring-security的学习网站希望对大家有帮助
http://www.mossle.com/docs/springsecurity3/html/springsecurity.html
https://vincentmi.gitbooks.io/spring-security-reference-zh/content/1_introduction.html
http://wiki.jikexueyuan.com/project/spring-security/log-in.html
spring-security4.1.2的学习的更多相关文章
- Spring 4 官方文档学习(十二)View技术
关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...
- Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC
内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...
- Spring JdbcTemplate 的使用与学习(转)
紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...
- Spring Security4实例(Java config版)——ajax登录,自定义验证
本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...
- Spring Security4实例(Java config 版) —— Remember-Me
本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...
- Spring Security4.1.3实现拦截登录后向登录页面跳转方式(redirect或forward)返回被拦截界面
一.看下内部原理 简化后的认证过程分为7步: 用户访问网站,打开了一个链接(origin url). 请求发送给服务器,服务器判断用户请求了受保护的资源. 由于用户没有登录,服务器重定向到登录页面 填 ...
- spring security4.2.2的maven配置+spring-security配置详解+java源码+数据库设计
最近项目需要添加权限拦截,经讨论决定采用spring security4.2.2!废话少说直接上干货! 若有不正之处,请谅解和批评指正,不胜感激!!!!! spring security 4.2.2文 ...
- Spring.NET依赖注入框架学习--实例化容器常用方法
Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
- Spring.NET依赖注入框架学习--简介
Spring.NET依赖注入框架学习--Spring.NET简介 概述 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入. ...
随机推荐
- Django中Form的Textarea字段
开始以为是这个样子: class BlogForm(forms.Form): title = forms.CharField(required = True) content = forms. ...
- Nginx-解读内置非默认模块 ngx_http_stub_status_module
1.Background ngx_http_stub_status_module 是一个 Nginx 的内置 HTTP 模块,该模块可以提供 Nginx 的状态信息.默认情况下这个模块是不被编译进来的 ...
- MyBatis代码自动生成(利用eclipse插件)
上一篇文章已经介绍了利用命令的方式自动生成mybatis代码,但是每次都去运行cmd命令感觉还是有点麻烦,所以找了些资料发现eclipse里面也可以安装插件自动生成代码,下面简单介绍一下,也是给自己以 ...
- Tomcat启动脚本
记录一个比较好的tomcat启动脚本,截取<OneinStack>,修改如下两个参数即可用. 使用之前修改下面2个参数: #Location of JAVA_HOME (bin files ...
- ILGenerator.Emit动态 MSIL编程(一)之基础
首先在Framework中,Emit相关的类基本都存在于System.Reflection.Emit命名空间下.可见Emit是作为反射的一个元素存在的. Emit能够实现什么?为什么要学习Emit?首 ...
- [2014.01.27]WFsoft.wfLibrary.wfIniFile 1.5
完全支持.net 2.0编写,对下一代操作系统平稳过渡. 不使用[DllImport("kernel32")]的方式,完全自主的.net 2.0自主解析. 完整支持键 ...
- linux---mysql远程访问
1.远程连接上Linux系统,确保Linux系统已经安装上了MySQL数据库. 登陆数据库.mysql -uroot -p(密码). 2.创建用户用来远程连接 GRANT ALL PRIVILEGES ...
- Laravel项目目录结构说明
Laravel项目目录结构说明: |- vendor 目录包含你的 Composer 依赖模块及laravel框架. |- bootstrap 目录包含几个框架启动跟自动加载配置的文件. |- app ...
- 黄聪:日租VPS中FileZilla_Server配置方法
1.关闭VPS中IIS的FTP服务 2.FileZilla_Server 监听端口 21 3.FTP客户端端口为11311(看服务商给出的)
- 轨迹记录App是怎样对定位轨迹进行过滤、优化和平滑处理的
https://www.zhihu.com/question/39983016 卡尔曼滤波原理 卡尔曼滤波学习笔记 卡尔曼滤波的原理说明 http://www.cs.unc.edu/~welch/ka ...