栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Security在所有角色名称中添加了前缀“ ROLE_”?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Security在所有角色名称中添加了前缀“ ROLE_”?

在Spring 4中,有两个方法,

hasAuthority()
并且
hasAnyAuthority()
org.springframework.security.access.expression.SecurityexpressionRoot
类中定义。这两种方法仅检查你的自定义角色名称,而不添加
ROLE_
前缀。定义如下:

public final boolean hasAuthority(String authority) {    return hasAnyAuthority(authority);}public final boolean hasAnyAuthority(String... authorities) {    return hasAnyAuthorityName(null, authorities);}private boolean hasAnyAuthorityName(String prefix, String... roles) {    Set<String> roleSet = getAuthoritySet();    for (String role : roles) {        String defaultedRole = getRoleWithDefaultPrefix(prefix, role);        if (roleSet.contains(defaultedRole)) { return true;        }    }    return false;}private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String role) {    if (role == null) {        return role;    }    if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) {        return role;    }    if (role.startsWith(defaultRolePrefix)) {        return role;    }    return defaultRolePrefix + role;}

用法示例:

<http auto-config="false" use-expressions="true" pattern="/user/**"      entry-point-ref="loginUrlAuthenticationEntryPoint">    <!--If we use hasAnyAuthority, we can remove ROLE_ prefix-->    <intercept-url pattern="/user/home/yoneticiler" access="hasAnyAuthority('FULL_ADMIN','ADMIN')"/>    <intercept-url pattern="/user/home/addUser" access="hasAnyAuthority('FULL_ADMIN','ADMIN')"/>    <intercept-url pattern="/user/home/addUserGroup" access="hasAuthority('FULL_ADMIN')"/>    <intercept-url pattern="/user/home/deleteUserGroup" access="hasAuthority('FULL_ADMIN')"/>    <intercept-url pattern="/user/home/**" access="hasAnyAuthority('FULL_ADMIN','ADMIN','EDITOR','NORMAL')"/>    <access-denied-handler error-page="/403"/>    <custom-filter position="FORM_LOGIN_FILTER" ref="customUsernamePasswordAuthenticationFilter"/>    <logout logout-url="/user/logout" invalidate-session="true" logout-success-url="/user/index?logout"/>    <!-- enable csrf protection -->    <csrf/></http>   <beans:bean id="loginUrlAuthenticationEntryPoint" >    <beans:constructor-arg value="/user"/></beans:bean>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号