项目场景:

项目springboot+security+vue前后端分离,业务需要整合activit7流程引擎


问题描述

在查询已办任务时,发现请求被限制无访问权限,


原因分析:

由于activiti7本身整合了springSecurity的安全框架,发现在请求业务层,调用了activti7的TaskRunTime,最终发现实现类上加了权限验证

@PreAuthorize("hasRole('ACTIVITI_USER')")
public class TaskRuntimeImpl implements TaskRuntime {
    ...
}

而项目本身并没有加这个权限标识,导致 无权访问


解决方案:

给需要的用户加上这个权限即可

由于项目是用的若依的前后端分离的基础版本,本身采用的springSecurity,所以找到UserDetail实现类,发现getAuthorities方法返回null,

发现原来若依的权限认证是用自己的方式实现的,所以权限认证的getAuthorities方法,需要自己改动,最终只要给需要的用户加上授权即可

public class LoginUser implements UserDetails {

    /**
     * 权限列表
     */
    private Set<String> permissions;

    //增加权限认证
    //此处是让redis不序列化SimpleGrantedAuthority对象,否则会序列化LoginUser异常,导致登录异常
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;
    
    ...
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        if(authorities != null) {
            return authorities;
        }

        authorities =             
                 
        permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());

        return authorities;
    }
}

在登录认证时,给需要的用户加上 ACTIVITI_USER, 由于activiti7本身整合security版本是2.5.14的,所以权限标识前默认需要加上ROLE_

@Service
public class UserDetailsServiceImpl implements UserDetailsService
{

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        //...

        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user)
    {
        
        Set<String> permissions = permissionService.getMenuPermission(user);
        //此处可结合自己业务,给需要的用户加上activiti权限
        permissions.add("ROLE_ACTIVITI_USER");
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissions);
    }
}

记得JWT认证加上authorities

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
        {
            tokenService.verifyToken(loginUser);
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        }
        chain.doFilter(request, response);
    }

}

至此,问题解决

Logo

前往低代码交流专区

更多推荐