How to reload authorities on user update with Spring Security

前端 未结 6 1842
情话喂你
情话喂你 2020-12-04 08:44

I\'m doing an application with authentication by OpenID using Spring Security. When user is logged-in, some authorities are loaded in his session.

I have User with f

6条回答
  •  庸人自扰
    2020-12-04 09:25

    I use the answer gived by TwiN, but I create a control variable (users_to_update_roles) to reduce performance impacts.

    @Component
    public class RoleCheckInterceptor implements HandlerInterceptor {
    public static ArrayList update_role = new ArrayList<>();
    
    @Autowired
    private IUser iuser;
    
    public static Set users_to_update_roles = new HashSet<>();
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
    
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    
        try {
    
            CurrentUser current = (CurrentUser) auth.getPrincipal();
    
            String username = current.getUser().getUsername();
            if (users_to_update_roles.contains(username)) {
                updateRoles(auth, current);
                users_to_update_roles.remove(username);
            }
    
        } catch (Exception e) {
            // TODO: handle exception
        }
    
        return true;
    }
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    
    }
    
    private void updateRoles(Authentication auth, CurrentUser current) {
        User findOne = iuser.findOne(current.getUser().getUsername());
        List updatedAuthorities = new ArrayList<>();
        for (Role role : findOne.getRoles()) {
            updatedAuthorities.add(new SimpleGrantedAuthority(role.name()));
        }
    
        Authentication newAuth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(),
                updatedAuthorities);
    
        SecurityContextHolder.getContext().setAuthentication(newAuth);
    }
    }
    

    and in my controller, I add the user that have they role updated

        public ModelAndView roleSave(@PathVariable long numero_documento, Funcionario funcionario) {
        ModelAndView modelAndView = new ModelAndView("funcionario/role");
        Set roles = funcionario.getPessoa().getUser().getRoles();
        funcionario = funcionarioService.funcionarioNumero_documento(numero_documento);
        funcionario.getPessoa().getUser().setRoles(roles);
        iUser.save(funcionario.getPessoa().getUser());
        RoleCheckInterceptor.users_to_update_roles.add(funcionario.getPessoa().getUser().getUsername());
        modelAndView.addObject("funcionario", funcionario);
        modelAndView.addObject("sucess", "Permissões modificadas");
        return modelAndView;
    }
    

提交回复
热议问题