Spring security authentication based on request parameter

前端 未结 2 704
盖世英雄少女心
盖世英雄少女心 2020-12-13 16:06

The application I\'m working on already has Spring Security to handle form based authentication. Now the requirement is to login a user programmatically via an external serv

2条回答
  •  北海茫月
    2020-12-13 16:44

    If you use Spring MVC controller or service, where targe request parameter is passed, then you can use @PreAuthorize Spring security annotation.

    Say, you have some Spring service that can check passed token and perform authentication if passed token is valid one:

    @Service("authenticator")
    class Authenticator {        
    ...
    public boolean checkTokenAndAuthenticate(Object token) {
        ...
        //check token and if it is invalid return "false"
        ...
        //if token is valid then perform programmatically authentication and return "true"  
    }
    ...             
    }    
    

    Then, with Spring security @PreAuthorize annotation you can do this it next way:

    ...
    @PreAuthorize("@authenticator.checkTokenAndAuthenticate(#token)")
    public Object methodToBeChecked(Object token) { ... }
    ...
    

    Also, you should enable Spring security annotations by and add spring-security-aspects to POM (or jar to classpath).

提交回复
热议问题