Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613627
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-04-27 14:45:49


点击(此处)折叠或打开


  1. package org.flowable.ui.common.security;

  2. import org.fh.util.Jurisdiction;
  3. import org.flowable.common.engine.api.FlowableIllegalStateException;
  4. import org.flowable.idm.api.User;
  5. import org.flowable.ui.common.model.RemoteUser;
  6. import org.springframework.security.core.Authentication;
  7. import org.springframework.security.core.context.SecurityContext;
  8. import org.springframework.security.core.context.SecurityContextHolder;

  9. import java.util.ArrayList;
  10. import java.util.List;

  11. /**
  12.  * 说明:重构流程编辑器获取用户信息
  13.  * 作者:FH Admin
  14.  * from:fhadmin.cn
  15.  */
  16. public class SecurityUtils {

  17.     private static User assumeUser;
  18.     
  19.     private static SecurityScopeProvider securityScopeProvider = new FlowableSecurityScopeProvider();

  20.     private SecurityUtils() {
  21.     }

  22.     /**
  23.      * Get the login of the current user.
  24.      */
  25.     public static String getCurrentUserId() {
  26.         User user = getCurrentUserObject();
  27.         if (user != null) {
  28.             return user.getId();
  29.         }
  30.         return null;
  31.     }

  32.     /**
  33.      * @return the {@link User} object associated with the current logged in user.
  34.      */
  35.     public static User getCurrentUserObject() {
  36.         if (assumeUser != null) {
  37.             return assumeUser;
  38.         }

  39.         RemoteUser user = new RemoteUser();
  40.         user.setId(Jurisdiction.getUsername());
  41.         user.setDisplayName(Jurisdiction.getName());
  42.         user.setFirstName(Jurisdiction.getName());
  43.         user.setLastName(Jurisdiction.getName());
  44.         user.setEmail("admin@flowable.com");
  45.         user.setPassword("123456");
  46.         List<String> pris = new ArrayList<>();
  47.         pris.add(DefaultPrivileges.ACCESS_MODELER);
  48.         pris.add(DefaultPrivileges.ACCESS_IDM);
  49.         pris.add(DefaultPrivileges.ACCESS_ADMIN);
  50.         pris.add(DefaultPrivileges.ACCESS_TASK);
  51.         pris.add(DefaultPrivileges.ACCESS_REST_API);
  52.         user.setPrivileges(pris);
  53.         return user;
  54.     }
  55.     
  56.     public static void setSecurityScopeProvider(SecurityScopeProvider securityScopeProvider) {
  57.         SecurityUtils.securityScopeProvider = securityScopeProvider;
  58.     }

  59.     public static SecurityScope getCurrentSecurityScope() {
  60.         SecurityContext securityContext = SecurityContextHolder.getContext();
  61.         if (securityContext != null && securityContext.getAuthentication() != null) {
  62.             return getSecurityScope(securityContext.getAuthentication());
  63.         }
  64.         return null;
  65.     }

  66.     public static SecurityScope getSecurityScope(Authentication authentication) {
  67.         return securityScopeProvider.getSecurityScope(authentication);
  68.     }

  69.     public static SecurityScope getAuthenticatedSecurityScope() {
  70.         SecurityScope currentSecurityScope = getCurrentSecurityScope();
  71.         if (currentSecurityScope != null) {
  72.             return currentSecurityScope;
  73.         }
  74.         throw new FlowableIllegalStateException("User is not authenticated");
  75.     }

  76.     public static void assumeUser(User user) {
  77.         assumeUser = user;
  78.     }

  79.     public static void clearAssumeUser() {
  80.         assumeUser = null;
  81.     }
  82. }


阅读(882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~