Spring Security控制权限
Spring Security控制权限
1,配置过滤器
为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了。
<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>
2,使用命名空间
在ApplicationContext.XML中配置或者另外单独使用一个Security.XML都可以,我们这里使用单独的xml来对Security进行配置.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" 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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> < /beans>
我们通常使用"security"作为默认的命名空间,而不是"beans",这意味着我们可以省略所有security命名空间元素的前缀,使上下文更容易阅读
<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-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> < /beans:beans>
3,配置认证提供接口和用户信息(这里使用内存用户)
<global-method-security pre-post-annotations="enabled">
</global-method-security>
<http use-expressions="true" access-denied-page="/AccessDenied.jsp">
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<form-login />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="rod" password="rod"
authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
</user-service>
</authentication-provider>
</authentication-manager>
<beans:bean id="loggerListener"
class="org.springframework.security.authentication.event.LoggerListener" />
这里配置pre-post-annotations="enabled"可以在接口函数(必须在接口中声明)/页面使用标签/表达式进行权限判断
- 如下表达式要求用户必须具备ROLE_SUPERVISOR角色才能调用
import org.springframework.security.access.prepost.PreAuthorize;
import sshDemo.Entities.*;
public interface IOwnerService {
@PreAuthorize("hasRole('ROLE_SUPERVISOR')")
public List<Owners> getOwners();
}
表达式基于
org.springframework.security.access.expression.SecurityExpressionRoot类提供权限判断.
- 另外也可以在页面中使用授权标签如下的sec:authorize要求具备ROLE_SUPERVISOR或者ROLE_TELLER才能使用该链接.
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="hasAnyRole('ROLE_SUPERVISOR','ROLE_TELLER')">
<a href="<s:url action='listAction'/>">List</a>
</sec:authorize>
security:authorize 标签声明下面的属性:
- ifAllGranted:这个标签列出的所有角色必须授权,才能输出标签的内容。
- ifAnyGranted:任何一个这个标签列出的角色必须授权,才能输出标签的内容。
- ifNotGranted:这个标签列出的角色没有一个是授权的,才能输出标签的内容。
标签sec:authorize的使用
authorize标签判断顺序是: access->url->ifNotGranted->ifAllGranted->ifAnyGranted 但他们的关系是“与”: 即只要其中任何一个属性不满足则该标签中间的内容将不会显示给用户,举个例子:
<sec:authorize ifAllGranted=”ROLE_ADMIN,ROLE_MEMBER” ifNotGranted=”ROLE_SUPER”>满足才会显示给用户 </sec:authorize>
标签中间的内容只有在当前用户拥有ADMIN,MEMBER角色,但不拥有SUPER权限时才会显示
满足ifAllGranted: 只需要grantedAuths.containsAll(requiredAuths);返回true即可
满足ifAnyGranted: 只需要grantedAuths.retainAll(requiredAuths);有内容即可(两集合有交集)
满足ifNotGranted:与Any相反,如果没有交集即可
3,使用自定义登陆页面
创建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
<head>
<base href="<%=basePath%>">
<title>Login</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<c:if test="${param.error}">
<font color="red"> Your login attempt was not successful, try
again.<br />
<br />
Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
</font>
</c:if>
<form action="${pageContext.request.contextPath}/j_spring_security_check" style="width:260px;text-align:center;" method="post">
<fieldset>
<legend>登陆</legend>
用户: <input type="text" name="j_username" style="width:150px;" value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}"/><br />
密码: <input type="password" name="j_password" style="width:150px;" /><br />
<input type="checkbox" name="_spring_security_remember_me" />两周之内不必登陆<br />
<input type="submit" value="登陆"/>
<input type="reset" value="重置"/>
</fieldset>
</form>
</body>
</html>
配置登陆界面
<http>
...
<form-login login-page="/login.jsp"
authentication-failure-url="/login.jsp?error=true"
default-target-url="/" />
...
</http>
4,配置退出操作
在页面中添加退出操作
<a href="j_spring_security_logout">Logout</a>
配置退出操作
<http>
...
<logout logout-success-url="/login.jsp" />
...
</http>
5,配置访问拒绝操作
创建访问拒绝页面
<%@page contentType="text/html; charset=GBK" %>
<html>
<head></head>
<body>
<div id="header"></div>
<div id="content" style="width: 60%">
<strong>Access Denied</strong>
</div>
</body>
</html>
配置访问拒绝重定向页面
<http access-denied-page="/AccessDenied.jsp">
...
</http>
6,运行调试
注意事项:
1,如果跟Struts2结合使用需要将spring security的配置放在struts2前面防止struts2将相应的处理截取出现如下错误:
Q:为何登录时出现There is no Action mapped for namespace / and action name j_spring_security_check.
A:这是因为登陆所发送的请求先被struts2的过滤器拦截了,为了试登陆请求可以被Spring Security正常处理,需要在web.xml中将Spring Security的过滤器放在struts2之前。
2,Struts结合Spring Security使用时出现Access Denied但是没有跳到对应的AccessDenied.jsp页面.
Q:Access Denied但是没有跳到对应的AccessDenied.jsp页面.
A:解决方法还没有
Spring Security控制权限的更多相关文章
- request.getRemoteUser() Spring Security做权限控制后
一. request.getRemoteUser();//获取当前缓存的用户,比如Spring Security做权限控制后就会将用户登录名缓存到这里 request.getRemoteAddr(); ...
- Spring Boot中使用 Spring Security 构建权限系统
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- 使用Spring Security实现权限管理
使用Spring Security实现权限管理 1.技术目标 了解并创建Security框架所需数据表 为项目添加Spring Security框架 掌握Security框架配置 应用Security ...
- spring security控制session
spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...
- 在Spring Boot中使用Spring Security实现权限控制
丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.so ...
- Spring Boot整合实战Spring Security JWT权限鉴权系统
目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以.像以前做项目的安全认证基于 session 的登录拦截,属于后端全栈式的开发的模式 ...
- spring security 控制用户信息用户加密 缓存用户信息
1. MD5加密 任何一个正式的企业应用中,都不会在数据库中使用明文来保存密码的,我们在之前的章节中都是为了方便起见没有对数据库中的用户密码进行加密,这在实际应用中是极为幼稚的做法.可以想象一下,只要 ...
- Spring Boot 集成 Spring Security 实现权限认证模块
作者:王帅@CodeSheep 写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...
随机推荐
- 安卓开发:一种快速提取安卓app的UI图标资源的方法
在做安卓设计时,找美工设计界面的漂亮图标是必不可少的,但是对于一个初创团队来说,请一个UI的成本其实也挺高的,此时对于一个偏技术的产品经理来说,从其他成熟的产品的apk中提取图标就是一个很便捷的方法, ...
- scala速成记录1
选择 Learning Scala这本书,两百多页,足够薄. 安装 http://www.scala-lang.org/ 下载Binary的版本.bin里边有所有操作系统下运行的可以运行的交互式s ...
- 调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。的解决方案
第一次调用webapi出错如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// ...
- RequireJS shim 用法说明
RequireJS中如果使用AMD规范,在使用的过程中没有太多的问题,如果加载非AMD规范的JS文件,就需要使用Require中的shim. require.config({ paths:{ jque ...
- 【JavaScript】图片上传预览
上传文件实时显示[一张图片]: 个人理解:给img的src传值:这个值就是input[type='file']的value: 不过你要判断浏览器类型[很多]:IE6.0,IE7/8/9,Fixfox7 ...
- 简单所以不要忽视,关于\r\n和\n程序员应了解的实际应用
众所周知,\r叫回车符,\n叫换行符. 由于历史原因,windows环境下的换行符是\r\n;(文章最后会稍微解释这个历史原因) linux和html等开源或公开标准中的换行符是\n. 记录这篇笔记的 ...
- Django登录访问限制 login_requeired
作用: 1. 用户登录之后才可以访问某些页面 2. 如果没登录,跳转到登录页面 3. 用户在跳转的登陆界面中完成登陆后,自动访问跳转到之前访问的地址 要实现这个需求很简单就是在相应的view前面使用装 ...
- jQuery控制表头
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 有用的git建议
这篇文章的目的是给经常使用git管理项目提供一个有益的提醒.如果你是git新手,可以先阅读文后的引用部分,然后在回头阅读此篇文章.在介绍git命令之前,你可以先看看来自 on-my-zsh 提供的别名 ...