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控制权限的更多相关文章

  1. request.getRemoteUser() Spring Security做权限控制后

    一. request.getRemoteUser();//获取当前缓存的用户,比如Spring Security做权限控制后就会将用户登录名缓存到这里 request.getRemoteAddr(); ...

  2. Spring Boot中使用 Spring Security 构建权限系统

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...

  3. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  4. 使用Spring Security实现权限管理

    使用Spring Security实现权限管理 1.技术目标 了解并创建Security框架所需数据表 为项目添加Spring Security框架 掌握Security框架配置 应用Security ...

  5. spring security控制session

    spring security控制session本文给你描述在spring security中如何控制http session.包括session超时.启用并发session以及其他高级安全配置. 创 ...

  6. 在Spring Boot中使用Spring Security实现权限控制

    丢代码地址 https://gitee.com/a247292980/spring-security 再丢pom.xml <properties> <project.build.so ...

  7. Spring Boot整合实战Spring Security JWT权限鉴权系统

    目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以.像以前做项目的安全认证基于 session 的登录拦截,属于后端全栈式的开发的模式 ...

  8. spring security 控制用户信息用户加密 缓存用户信息

    1. MD5加密 任何一个正式的企业应用中,都不会在数据库中使用明文来保存密码的,我们在之前的章节中都是为了方便起见没有对数据库中的用户密码进行加密,这在实际应用中是极为幼稚的做法.可以想象一下,只要 ...

  9. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

随机推荐

  1. 读取MP3专辑图片

    #define WIN32_LEAN_AND_MEAN #define NOWINRES #define NOSERVICE #define NOMCX #define NOIME #include ...

  2. Linq to Xml读取复杂xml(带命名空间)

    前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...

  3. Maven使用archetype迅速生成项目骨架

    archetype意思是"原型",相当于项目模板.archetype是maven的一个插件,相当于模板工具包. 一个十分重要的mvn指令:mvn 插件名:目标名maven自带三个内 ...

  4. VS2010 MFC对Excel的操作

    这是帮别人做项目遇到的一个问题,的那个是纠结了老长时间,本以为是一件很轻松的事... 首先,这里采用了OLE来对Excel进行操作,网上其实有大把的例子,虽然都可以运行,但是并不能满足项目要求,其实我 ...

  5. Python操作Mysql之基本操作

    pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...

  6. [Android]Volley源码分析(五)

    前面几篇通过源码分析了Volley是怎样进行请求调度及请求是如何被实际执行的,这篇最后来看下请求结果是如何交付给请求者的(一般是Android的UI主线程). 类图:

  7. 机器学习笔记----四大降维方法之PCA(内带python及matlab实现)

    大家看了之后,可以点一波关注或者推荐一下,以后我也会尽心尽力地写出好的文章和大家分享. 本文先导:在我们平时看NBA的时候,可能我们只关心球员是否能把球打进,而不太关心这个球的颜色,品牌,只要有3D效 ...

  8. Win10---------专区

    待完善中---------------------------------- -----------------------------------------The End------------- ...

  9. flask-whooshalchemy需要注意的一点

    在学习mega—tutorial时全文搜索模块遇到了问题,那就是使用全文搜索查询出来的数据为空的列表,输出了sql语句后发现where后没有条件,困扰了许久,后来才发现是自己不细心,在进行全文索引时应 ...

  10. Fibonacci 数列算法分析

    /************************************************* * Fibonacci 数列算法分析 ****************************** ...