Do I need resource server with Spring Security OAuth2?

前端 未结 3 1900
时光取名叫无心
时光取名叫无心 2020-12-20 01:36

I am trying implement OAuth2 authentication with JWT tokens. If I understand, I need send credentials to authorization server, this verify my credentials, and return back si

3条回答
  •  轮回少年
    2020-12-20 01:50

    Yes, you will want to configure the resources protected by your JWT's by extending ResourceServerConfigurerAdapter. A basic implementation might look like this

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        }
    
    }
    

    This means you should have no need to extend WebSecurityConfigurerAdapter because the above configuration configures the same HttpSecurity object that you would be configuring in WebSecurityConfigurerAdapter. The public void configure(HttpSecurity http) works on the same thing in both classes.

    The reason we want to choose ResourceServerConfigurerAdapter over WebSecurityConfigurerAdapter is because it's part of the spring-security-oauth2 module that you are using, and will be used behind the scenes by the framework.

    You will of course need to make sure that you are using the same signing key for both your authorization and resource servers. If you are defining your security config beans in the same application the resource server will automatically use the same beans, if not then you will need to duplicate whatever JWT related config you have on your authorization server.

提交回复
热议问题