spring-security(2)
记录一下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">
<!-- 指定登录页面不拦截 -->
<http security="none" pattern="/login.htm"/>
<http auto-config="true">
<!--
login-page 指定登录页面
username-parameter 登录的用户名,默认是“j_username”
password-parameter 登录的密码,默认是“j_password”
login-processing-url 登录提交的页面,默认是“/j-spring-security-check”
default-target-url 指定登录成功后跳转的界面
authentication-success-handler-ref 指定登录成功后调用的服务,这里指定后default-target-url就不生效, AuthenticationSuccessHandler
authentication-failure-url 指定登录失败后跳转的界面
authentication-failure-handler-ref="authenticationFailHandler" 指定登录失败后调用的服务,这里指定后authentication-failure-url就不生效, SimpleUrlAuthenticationFailureHandler
-->
<form-login
login-page="/login.htm"
username-parameter="username"
password-parameter="password"
login-processing-url="/spring-security-check"
default-target-url="/user/welcome.htm"
authentication-success-handler-ref="authenticationSuccessHandler"
authentication-failure-url="/user/fail.htm"
authentication-failure-handler-ref="authenticationFailHandler"
/>
<!--禁用CSRF保护功能 默认开启-->
<csrf disabled="true"/>
<!--
退出登录配置
logout-url:退出登录提交的页面,默认j_spring_security_logout
success-handler-ref: 成功退出登录的事件 LogoutSuccessHandler
-->
<logout logout-url="/spring_security_logout" success-handler-ref="logoutSuccessHandler" />
<!--intercept-url定义了一个权限控制的规则。
pattern:进行权限控制的url
access:需要什么权限,以逗号分隔的角色列表,只需拥有其中的一个角色就能成功访问
-->
<intercept-url pattern="/" access="hasRole('role1')" />
<intercept-url pattern="/*.htm" access="hasRole('role1')"/>
<intercept-url pattern="/**/.htm" access="hasRole('role1')"/>
</http>
<!--开启权限注解支持 -->
<global-method-security secured-annotations="enabled" jsr250-annotations="enabled"/>
<beans:bean id="userService" class="com.yitop.feng.service.UserService"/>
<beans:bean id="authenticationSuccessHandler" class="com.yitop.feng.service.security.AuthenticationSuccessHandlerImpl" />
<beans:bean id="authenticationFailHandler" class="com.yitop.feng.service.security.AuthenticationFailHandlerImpl" />
<!--
authentication-manager元素指定了一个AuthenticationManager,其需要一个AuthenticationProvider来进行真正的认证,
默认情况下authentication-provider对应一个UserDetailsService来获取用户信息(即查询数据库)。
-->
<authentication-manager>
<authentication-provider user-service-ref="userService">
<!--
hash 指定密码加密方式 plaintext sha sha-256 md4 md5 {sha} {ssha}
加密算法 PasswordEncoder 实现类 plaintext PlaintextPasswordEncoder
sha ShaPasswordEncoder
sha-256 ShaPasswordEncoder,使用时new ShaPasswordEncoder(256)
md4 Md4PasswordEncoder
md5 Md5PasswordEncoder
{sha} LdapShaPasswordEncoder
{ssha} LdapShaPasswordEncoder
base64 表示是否需要对加密后的密码使用BASE64进行编码,默认是false
-->
<password-encoder hash="md5" base64="true"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
url权限控制表达式
hasRole([role]) 当前用户是否拥有指定角色。
hasAnyRole([role1,role2]) 多个角色是一个以逗号进行分隔的字符串。如果当前用户拥有指定角色中的任意一个则返回true。
hasAuthority([auth]) 等同于hasRole
hasAnyAuthority([auth1,auth2]) 等同于hasAnyRole
Principle 代表当前用户的principle对象
authentication 直接从SecurityContext获取的当前Authentication对象
permitAll 允许所有
denyAll 拒绝所有
注解功能
<!-- 开启注解功能 -->
<security:global-method-security jsr250-annotations="enabled"/>
@RolesAllowed({"ROLE_USER", "ROLE_ADMIN"})
public User find(int id) {
System.out.println("find user by id............." + id);
return null;
}
//允许ROLE_USER或ROLE_ADMIN使用find方法
//@PermitAll 允许所有
//@DenyAll 拒绝所有
spring-security(2)的更多相关文章
- Spring Security OAuth2 开发指南
官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...
- spring mvc 和spring security配置 web.xml设置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
- 【OAuth2.0】Spring Security OAuth2.0篇之初识
不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...
- spring security oauth2.0 实现
oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...
- Spring Security(08)——intercept-url配置
http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...
- Spring Security控制权限
Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...
- Spring Security笔记:Hello World
本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...
- Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken
在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...
- spring session 和 spring security整合
背景: 我要做的系统前面放置zuul. 使用自己公司提供的单点登录服务.后面的业务应用也是spring boot支撑的rest服务. 目标: 使用spring security管理权限包括权限.用户请 ...
随机推荐
- DB2事务日志
1.DB2数据库的日志原理 事务日志记录数据库中所有对象和数据的改变,在早前版本中最大可达256G,其大小为( logprimary + logsecond ) * logfilsiz,其中logpr ...
- Luogu 3960 [NOIP2017] 列队 - splay|线段树
题解 是我从来没有做过的裂点splay... 看的时候还是很懵逼的QAQ. 把最后一列的$n$个数放在一个平衡树中, 有 $n$ 个点 剩下的$n$行数, 每行都开一个平衡树,开始时每棵树中仅有$1$ ...
- Tomcat连接池配置
今日做了个小网站,数据量不大,但当发布到虚拟主机上之后,接连不断的遇到各种问题. 被折磨了数日后,在网上查了大量的相关资料,现总结如下. 一.项目在上传到远程服务器的过程中,有可能丢失文件,或文件内容 ...
- C# 编写最小化时隐藏为任务栏图标的Window appllication
1.设置窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小化事件(首先需 ...
- Centos记录所有用户登录和操作的详细日志
1.起因 最近Linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录. 一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行 ...
- 2018.10.13 bzo1934: [Shoi2007]Vote 善意的投票(最小割)
传送门 最小割定义题. 按照题意建边就行了. 考虑把冲突变成把aaa选入不与自己匹配的集合所需要付出的代价. 然后跑最小割就行了. 代码: #include<bits/stdc++.h> ...
- class和struct
相同点 实际上可以使用这两个关键字定义任何一个类. 区别 1.struct的默认成员访问说明符为public,class的默认成员访问说明符为private(什么叫默认?就是没有写明public.pr ...
- 105032014138_牟平_z作业1
2)NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. ...
- JavaLogin小框架制作【精品博客】
做一个小登录接口方法,让用户传入用户名,密码,就可以知道登录的结果信息,并以接口监听的方式控制. 先看客户端执行效果: 输入正确: 输入错误: 模拟客户端使用登录小框架: package com.de ...
- 【C++】C++中的虚函数与纯虚函数
C++中的虚函数 先来看一下实际的场景,就很容易明白为什么要引入虚函数的概念.假设我们有一个基类Base,Base中有一个方法eat:有一个派生类Derived从基类继承来,并且覆盖(Override ...