zoukankan      html  css  js  c++  java
  • Spring Security3 页面 权限标签

    应用标签库:<%@ taglib prefix='security' uri='http://www.springframework.org/security/tags' %>

    <security:authorize>是一个流程控制标签,能够在满足特定安全需求的条件下显示它的内容体。它有三个互斥的参数:
    ifAllGranted——是一个由逗号分隔的权限列表,用户必须拥有所有列出的权限时显示;
    ifAnyGranted——是一个由逗号分隔的权限列表,用户必须至少拥有其中的一个权限时才能显示;
    ifNotGranted——是一个由逗号分隔的权限列表,用户未拥有所有列出的权限时才能显示。
    <security:authentication>获得属性的值比如要获得用户名可以这么写:
    <security:authentication property="principal.username"></security:authentication>
    他有三个属性,property是必须的,另外scope和var,var定义一个变量,scope定义var存在的范围
    例子:
    有时需要在页面显示用户名,或者根据用户角色显示或者不显示一些内容。这需要使用到spring security提供的标签库。

    在页面中引入标签库:

     

    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

    使用标签库的示例:

    <sec:authentication property="principal" var="authentication"/> <sec:authorize ifAllGranted="ROLE_USER">可以访问</sec:authorize> 用户名:${authentication.username }<br />
    前台 ROLE_ANONYMOUS表示匿名用户
    在配置文件中可以设置页面进入的权限
    <intercept-url pattern="/Homepage.*" access="ROLE_ADMIN,IS_AUTHENTICATED_ANONYMOUSLY"/>
    IS_AUTHENTICATED_ANONYMOUSLY允许匿名用户进入
    IS_AUTHENTICATED_FULLY 允许登录用户进入
    IS_AUTHENTICATED_REMEMBERED 允许登录用户和rememberMe用户进入
    IS_AUTHENTICATED_FULLY:是则满足以下情况返回通过: **.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的实例 IS_AUTHENTICATED_REMEMBERED:是则满足以下任一情况返回通过: a*.Authentication是RememberMeAuthenticationToken的实例 b*.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的实例 IS_AUTHENTICATED_ANONYMOUSLY:是则满足以下任一情况返回通过: a*.Authentication是AnonymousAuthenticationToken的实例 b*.既不是RememberMeAuthentication也不是AnonymousAuthenticationToken的实例 c*.Authentication是RememberMeAuthenticationToken的实例
     
     
     
     
    spring-security 在jsp中的标签库 1.在jsp中声明
    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    2.标签 目前共有三个标签    
    <sec:authorize></sec:authorize>      
    <sec:authentication property=""></sec:authentication>
    <sec:accesscontrollist hasPermission="" domainObject=""></sec:accesscontrollist>
        2.1、authorize标签 这个标签用来决定它的内容是否会被执行.
    <sec:authorize access="hasRole('supervisor')">
        This content will only be visible to users who have
        the "supervisor" authority in their list of GrantedAuthoritys.
    </sec:authorize>
    显示一个特定的链接,如果用户允许点击它.
    <sec:authorize url="/admin">
        This content will only be visible to users who are authorized to send requests to the "/admin" URL.
    </sec:authorize>
    2.2、authentication标签 这个标签允许访问当前的Authentication 对象, 保存在安全上下文中。 比如,如果Authentication 的principal 属性是Spring Security 的UserDetails 对象的一个实例, 就要使用
    <sec:authentication property="principal.username" /> 
    来渲染当前用户的名称。
    当然,它不必使用JSP 标签来实现这些功能,一些人更愿意在视图中保持逻辑越少越好。你可以在你的MVC 控制器中访问Authentication 对象( 通过调用 SecurityContextHolder.getContext().getAuthentication()) 然后直接在模型中添加数据,来渲染视图。
    2.3、accesscontrollist标签 这个标签纸在使用Spring Security ACL 模块时才可以使用。它检测一个用逗号分隔的特 定领域对象的需要权限列表。如果当前用户拥有这些权限的任何一个,标签内容就会被执行。 否则,就会被略过。
    <sec:accesscontrollist hasPermission="1,2" domainObject="${someObject}">
        This will be shown if the user has either of the permissions
        represented by the values "1" or "2" on the given object.
    </sec:accesscontrollist>





    applicationContext_security.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <b:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:b="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">
    	<http auto-config="true" access-denied-page="/accessDenied.jsp">
    		<!-- 不要过滤图片等静态资源  filters="none"-->
    		<intercept-url pattern="/**/*.jpg" filters="none" />
    		<intercept-url pattern="/**/*.png" filters="none" />
    		<intercept-url pattern="/**/*.gif" filters="none" />
    		<intercept-url pattern="/**/*.css" filters="none" />
    		<intercept-url pattern="/**/*.js" filters="none" />
    		
    		<!-- 登陆页和忘记密码或注册等不需要过滤的页面 -->
    		<intercept-url pattern="/login.jsp" filters="none" />
    		<intercept-url pattern="/jsp/forgotpassword.jsp"
    			filters="none" />
    
    		<form-login login-page="/login.jsp"
    			authentication-failure-url="/login.jsp?error=true"
    			default-target-url="/index.jsp" />
    		
    		<logout logout-success-url="/login.jsp" />
    
    		<!-- "记住我"功能,采用持久化策略(将用户的登录信息存放在数据库表中)需要创建一张persistent_logins 表 
    		<remember-me data-source-ref="dataSource" />
    
    		--><!-- 检测失效的sessionId,超时时定位到另外一个URL -->
    		<session-management invalid-session-url="/sessionTimeout.jsp" />
    
    		<!--
    			增加一个自定义的filter,放在FILTER_SECURITY_INTERCEPTOR之前,实现用户、角色、权限、资源的数据库管理。
    		-->
    		<custom-filter ref="myFilter" before="FILTER_SECURITY_INTERCEPTOR" />
    	</http>
    
    	<!--
    		一个自定义的filter
    		必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性。
    	-->
    	<b:bean id="myFilter" class="org.joshua.ss.MyFilterSecurityInterceptor">
    		<b:property name="authenticationManager" ref="authenticationManager" />
    		<b:property name="accessDecisionManager" ref="myAccessDecisionManager" />
    		<b:property name="securityMetadataSource" ref="mySecurityMetadataSource" />
    	</b:bean>
    
    	<!-- 注意能够为authentication-manager 设置alias别名  -->
    	<authentication-manager alias="authenticationManager">
    		<authentication-provider user-service-ref="myUserDetailService"><!--
    			 <password-encoder hash="md5" />
    		--></authentication-provider>
    	</authentication-manager>
    
    	<b:bean id="myUserDetailService" class="org.joshua.ss.MyUserDetailService" />
    
    	<!-- 访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源。11/3/23 -->
    	<b:bean id="myAccessDecisionManager"
    		class="org.joshua.ss.MyAccessDecisionManager">
    	</b:bean>  
    
    	<!-- 资源源数据定义,将所有的资源和权限对应关系建立起来,即定义某一资源可以被哪些角色去访问。11/3/23 -->
    	<b:bean id="mySecurityMetadataSource"
    		class="org.joshua.ss.MyInvocationSecurityMetadataSource">
    	</b:bean> 
    
    </b:beans>
    dbConfig.properties
    jdbc.user=scott
    jdbc.pwd=snail
    jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:oracle
    jdbc.driver=oracle.jdbc.driver.OracleDriver
    ehcache.xml 没有深入的研究,暂且搁置
    <?xml version="1.0" encoding="UTF-8" ?>
    <ehcache>
    	<diskStore path="user.dir"></diskStore>
    	<defaultCache 
    	maxElementsInMemory="10000"
    	eternal="false"
    	timeToIdleSeconds="120"
    	timeToLiveSeconds="120"
    	overflowToDisk="true" />
    </ehcache>
    




    如果只是想从页面上显示当前登陆的用户名,可以直接使用Spring Security提供的taglib。

    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> <div>username : <sec:authentication property="name"/></div>         如果想在程序中获得当前登陆用户对应的对象。

    UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()     .getAuthentication()     .getPrincipal();         如果想获得当前登陆用户所拥有的所有权限。

    GrantedAuthority[] authorities = userDetails.getAuthorities();

  • 相关阅读:
    fiddler如何抓取夜神模拟器上的包
    关于在虚拟机上安装iOS所遇到的问题
    ADB WiFi连接手机
    ADB命令总结(1)
    ADB连接手机遇到的问题:list of devices attached
    win 7 查看端口被占用
    APP测试用例要考虑的一些方面
    什么是阿尔法测试?
    关于ADB push 出现failed to copy 'D:file.xtxt' to '/system/temp/' : Read-only file system 的报错信息解决办法
    ADB push 和ADB pull命令
  • 原文地址:https://www.cnblogs.com/yaowen/p/2985848.html
Copyright © 2011-2022 走看看