activiti中报错An Authentication object was not found in the SecurityContext

深藏阁楼爱情的钟 2023-01-02 15:27 271阅读 0赞

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

这是因为activiti7与springboot整合中集成了springsecurity

虽然你的用户赋予了权限

但是你执行方法的时候没有定义是哪个用户在执行方法

要不就整完全点写登录页面

要不就自定义用户去执行方法

securityUtil.logInAs(“zhangsan”);

下面是这个SecurityUtil

  1. package com.qiangqiang.util;
  2. import org.slf4j.LoggerFactory;
  3. import org.slf4j.Logger;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.boot.autoconfigure.AutoConfigureOrder;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.GrantedAuthority;
  9. import org.springframework.security.core.context.SecurityContextHolder;
  10. import org.springframework.security.core.context.SecurityContextImpl;
  11. import org.springframework.security.core.userdetails.UserDetails;
  12. import org.springframework.security.core.userdetails.UserDetailsService;
  13. import org.springframework.stereotype.Component;
  14. import java.util.Collection;
  15. /**
  16. * \* Created with IntelliJ IDEA.
  17. * \* @author: xiyue
  18. * \* Date: 2021/1/6
  19. * \* Time: 11:00
  20. * \* To change this template use File | Settings | File Templates.
  21. * \* Description:
  22. * \ activiti与springboot整合之后,默认集成了spring security
  23. */
  24. @Component
  25. public class SecurityUtil {
  26. @Autowired
  27. @Qualifier("myUserDetailsService")
  28. private UserDetailsService userDetailsService;
  29. public void logInAs(String username) {
  30. UserDetails user = userDetailsService.loadUserByUsername(username);
  31. if (user == null) {
  32. throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
  33. }
  34. SecurityContextHolder.setContext(
  35. new SecurityContextImpl(
  36. new Authentication() {
  37. @Override
  38. public Collection<? extends GrantedAuthority> getAuthorities() {
  39. return user.getAuthorities();
  40. }
  41. @Override
  42. public Object getCredentials() {
  43. return user.getPassword();
  44. }
  45. @Override
  46. public Object getDetails() {
  47. return user;
  48. }
  49. @Override
  50. public Object getPrincipal() {
  51. return user;
  52. }
  53. @Override
  54. public boolean isAuthenticated() {
  55. return true;
  56. }
  57. @Override
  58. public void setAuthenticated(boolean b) throws IllegalArgumentException {
  59. }
  60. @Override
  61. public String getName() {
  62. return user.getUsername();
  63. }
  64. }
  65. )
  66. );
  67. org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
  68. }
  69. }

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读