Cas 5.2.4 using REST API to return a 401 Unauthorized error, solve

Dependencies have been added according to the instructions on REST Protocol on the official website.

public class CustomAuthentionHandler extends AbstractPreAndPostProcessingAuthenticationHandler {
    @Autowired
    private UserServiceImpl userService;
    public CustomAuthentionHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory, Integer order){
        super(name,servicesManager,principalFactory,order);
    }
    @Override
    protected HandlerResult doAuthentication(Credential credential) throws GeneralSecurityException, PreventedException {
        System.out.println("\nCredential==="+credential.getClass().getName()+"\n");
        UsernamePasswordCredential usernamePasswordCredential = (UsernamePasswordCredential) credential;
        String username = usernamePasswordCredential.getUsername();
        String password = usernamePasswordCredential.getPassword();
        //
        UserVo userVo = userService.getUserWithMultiAttrs(username, password);
        System.out.println("userVo: "+userVo.toString());
        if(userVo==null){
            throw new AccountNotFoundException("");
        }
        //  
        Map<String, Object> map = new HashMap<>();  
        map.put("external\_userinfo", JSON.toJSONString(userVo));
        return createHandlerResult(usernamePasswordCredential, principalFactory.createPrincipal(username, map), null);
    }

    @Override
    public boolean supports(Credential credential) {
        return credential instanceof UsernamePasswordCredential;
    }
}
Mar.22,2021

has been resolved, because the REST API, included with: CAS Server only supports the default UsernamePasswordCredential , and my project extends the custom UsernamePasswordCredential , so you need to make a judgment in the custom verification Handler. If the Credential type of the request is org.apereo.cas.authentication.UsernamePasswordCredential , it comes from the REST request, and you can directly return the corresponding UsernamePasswordCredential object.

Menu