在页面中引入标签库:
使用标签库的示例:
<%@ 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" />来渲染当前用户的名称。
<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();