SpringSecurity鉴权 启用方法级安全控制 权限上下文传递工具 自定义权限服务 ss
启用方法级安全控制
package com. chinabuilder. framework. config ;
@EnableMethodSecurity ( prePostEnabled = true , securedEnabled = true )
@Configuration
public class SecurityConfig {
}
权限上下文传递工具
package com. chinabuilder. framework. security. context ; public class PermissionContextHolder
{ private static final String PERMISSION_CONTEXT_ATTRIBUTES = "PERMISSION_CONTEXT" ; public static void setContext ( String permission) { RequestContextHolder . currentRequestAttributes ( ) . setAttribute ( PERMISSION_CONTEXT_ATTRIBUTES, permission, RequestAttributes . SCOPE_REQUEST) ; } public static String getContext ( ) { return Convert . toStr ( RequestContextHolder . currentRequestAttributes ( ) . getAttribute ( PERMISSION_CONTEXT_ATTRIBUTES, RequestAttributes . SCOPE_REQUEST) ) ; }
}
自定义权限服务 ss
package com. chinabuilder. framework. web. service ; @Service ( "ss" )
public class PermissionService
{ public boolean hasPermi ( String permission) { if ( StringUtils . isEmpty ( permission) ) { return false ; } LoginUser loginUser = SecurityUtils . getLoginUser ( ) ; if ( StringUtils . isNull ( loginUser) || CollectionUtils . isEmpty ( loginUser. getPermissions ( ) ) ) { return false ; } PermissionContextHolder . setContext ( permission) ; return hasPermissions ( loginUser. getPermissions ( ) , permission) ; } public boolean lacksPermi ( String permission) { return hasPermi ( permission) != true ; } public boolean hasAnyPermi ( String permissions) { if ( StringUtils . isEmpty ( permissions) ) { return false ; } LoginUser loginUser = SecurityUtils . getLoginUser ( ) ; if ( StringUtils . isNull ( loginUser) || CollectionUtils . isEmpty ( loginUser. getPermissions ( ) ) ) { return false ; } PermissionContextHolder . setContext ( permissions) ; Set < String > authorities = loginUser. getPermissions ( ) ; for ( String permission : permissions. split ( Constants . PERMISSION_DELIMETER) ) { if ( permission != null && hasPermissions ( authorities, permission) ) { return true ; } } return false ; } public boolean hasRole ( String role) { if ( StringUtils . isEmpty ( role) ) { return false ; } LoginUser loginUser = SecurityUtils . getLoginUser ( ) ; if ( StringUtils . isNull ( loginUser) || CollectionUtils . isEmpty ( loginUser. getUser ( ) . getRoles ( ) ) ) { return false ; } for ( SysRole sysRole : loginUser. getUser ( ) . getRoles ( ) ) { String roleKey = sysRole. getRoleKey ( ) ; if ( Constants . SUPER_ADMIN. equals ( roleKey) || roleKey. equals ( StringUtils . trim ( role) ) ) { return true ; } } return false ; } public boolean lacksRole ( String role) { return hasRole ( role) != true ; } public boolean hasAnyRoles ( String roles) { if ( StringUtils . isEmpty ( roles) ) { return false ; } LoginUser loginUser = SecurityUtils . getLoginUser ( ) ; if ( StringUtils . isNull ( loginUser) || CollectionUtils . isEmpty ( loginUser. getUser ( ) . getRoles ( ) ) ) { return false ; } for ( String role : roles. split ( Constants . ROLE_DELIMETER) ) { if ( hasRole ( role) ) { return true ; } } return false ; } private boolean hasPermissions ( Set < String > permissions, String permission) { return permissions. contains ( Constants . ALL_PERMISSION) || permissions. contains ( StringUtils . trim ( permission) ) ; }
}