最近项目开发中有这样一个业务逻辑,一个登陆画面,根据不同权限跳转到不同的画面(Action)
开始的做法是直接跳到一个调度的Action,再由这个Action去分配。
这次开发使用了安全框架,遂希望通过安全框架去做这个调度
于是使用authentication-success-handler-ref
来替换default-target-url和always-use-default-target,实现这一目的
国际惯例,先上代码
<http auto-config='true' >
<intercept-url pattern="/public/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/user/**" access="ROLE_SPACE_ADMIN,ROLE_SMALL_SPACE_ADMIN,ROLE_INSTITUTION_MEMBER,ROLE_SYSTEM_ADMIN"/>
<intercept-url pattern="/admin/**" access="ROLE_SUPER_ADMIN"/>
<form-login login-page="/user/login.action"
authentication-failure-url="/user/login.action?msg=fault"
authentication-success-handler-ref="authenticationDispatcher"
login-processing-url="/securityLogin"/>
<logout logout-success-url="/user/login.action" logout-url="/securityLogout"/>
</http>
<beans:bean id="authenticationDispatcher" class="com.lstp.service.security.impl.LstpAuthenticationSuccessHandler">
<beans:property name="authDispatcherMap">
<beans:ref local="dispatcherMap"/>
</beans:property>
</beans:bean>
<beans:bean id="dispatcherMap" class="java.util.HashMap">
<beans:constructor-arg>
<beans:map>
<beans:entry key="ROLE_SPACE_ADMIN" value="/user/userSpace.action"/>
<beans:entry key="ROLE_SMALL_SPACE_ADMIN" value="/user/userSpace.action"/>
<beans:entry key="ROLE_INSTITUTION_MEMBER" value="/user/userSpace.action"/>
<beans:entry key="ROLE_SYSTEM_ADMIN" value="/admin/adminSpace.action"/>
<beans:entry key="ROLE_SUPER_ADMIN" value="/admin/adminSpace.action"/>
</beans:map>
</beans:constructor-arg>
</beans:bean>
authentication-success-handler-ref="authenticationDispatcher"是至关重要的,当登陆成功会调用实现AuthenticationSuccessHandler接口的onAuthenticationSuccess方法.
下面是实现类
package com.lstp.service.security.impl;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.util.Assert;
/**
* 权限登录成功句柄
* 该类为平台成功跳转到多个入口提供依据
* @author ryuu-kk
*
*/
public class LstpAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
/**
* url参数
*/
private Map<String, String> map;
/**
* 多role选择,默认取得权限表第一个权限
*/
private boolean isFirst = true;
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
Assert.notNull(map, "AuthInterceptMap is null!");
String url = "";
Collection<GrantedAuthority> authCollection = authentication.getAuthorities();
if (authCollection.isEmpty()) {
return;
}
//对于一个登录用户有多种角色,只取得第一个
if (isFirst) {
GrantedAuthority[] a = new GrantedAuthorityImpl[]{};
url = map.get(authCollection.toArray(a)[0].toString());
response.sendRedirect(request.getContextPath() + url);
return;
}
//选择取得最后一个role掉转;这里一个用户的多个角色较少
//迭代的速度比转换成数组的速度要快
for (GrantedAuthority auth : authCollection) {
url = map.get(auth.getAuthority());
}
response.sendRedirect(url);
}
/**
* 权限跳转依据
* @param map 参数
* key:url
* value:role
*/
public void setAuthDispatcherMap(Map<String, String> map) {
this.map = map;
}
/**
* 多种角色方案
* 设置是否只取得第一个role
* @param isFirst true:多种角色只取第一个,false:取得最后一个
*/
public void setMultipleAuth(boolean isFirst) {
this.isFirst = isFirst;
}
}
分享到:
相关推荐
springsecurity角色和权限
authentication-success-handler-ref B.1.5.7. authentication-failure-handler-ref B.1.6. <http-basic> 元素 B.1.7. <remember-me> 元素 B.1.7.1. data-source-ref B.1.7.2. token-repository-ref B....
authentication-success-handler-ref B.1.5.7. authentication-failure-handler-ref B.1.6. <http-basic>元素 B.1.7. <remember-me>元素 B.1.7.1. data-source-ref B.1.7.2. token-repository-ref B.1.7.3. ...
Spring Security 参考 1 第一部分前言 15 1.入门 16 2.介绍 17 2.1什么是Spring Security? 17 2.2历史 19 2.3版本编号 20 2.4获得Spring安全 21 2.4.1使用Maven 21 Maven仓库 21 Spring框架 22 2.4.2 Gradle 23 ...
#authentication representing the Spring Security authentication object (an object implementing the org.springframework.security.core.Authentication interface). #authorization: a expression utility ...
authentication-success-handler-ref B.1.5.7. authentication-failure-handler-ref B.1.6. <http-basic> 元素 B.1.7. <remember-me> 元素 B.1.7.1. data-source-ref B.1.7.2. token-repository-ref B....
Spring Security 演讲PPT(演讲嘉宾:张明星) WebSphere技术专家沙龙在广州圆满举办,WSC超级版主Fastzch(张明星)担任本次沙龙的演讲嘉宾,他给广州的WebSphere技术专家带来了以“Spring Security ”为主题的...
项目自身的权限信息结合spring security 框架的实现。 本DEMO只包括从数据库读取登录认证信息,认证通过后 从数据库读取授权信息来控制用户的访问.权限元素包括 用户,角色,菜单以及这三者的关系。 本DEMO使用了...
springsecurity是一个功能强大且高度可定制的身份验证和访问控制框架。springsecurity是一个...最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在spring security中一种过滤器处理一种认证方式。
Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。 Spring Security 为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业...
官方版本,亲测可用
官方版本,亲测可用
一个能够为基于Spring的企业应用系统提供声明式的安全訪问控制解决方式的安全框架(简单说是对访问权限进行控制嘛),应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。用户认证指...
Spring-Security-authentication-against-MongoDB-Using-Morphia 这是一个演示示例,它使用 Morphia API 演示了针对 mongoDB 的 Spring 身份验证 此示例使用带有 Gradle 插件的 STS IDE 开发。 要运行该应用程序,...
Spring Security是什么? 历史 发布版本号 Getting Spring Security Spring Security 4.1新特性 Java 配置提升 Web应用程序安全性提升 授权改进 密码模块的改进 测试的改进 一般的改进 样品和指南 (Start Here) Java ...
secure-boot-image-authentication-11.30.16
WG-Authentication-Gateway_12_3_1,WatchGuard 防火墙sso插件
jwt-react-authentication-源码.rar
Hands-On Spring Security 5 for Reactive Applications starts with the essential concepts of reactive programming, Spring Framework, and Spring Security. You will then learn about a variety of ...
Laravel开发-laravel-authentication-acl Laravel身份验证和带有酷管理面板的acl包