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 signed JWT token. Next I tried implement WebSecurityConfig
which extends WebSecurityConfigurerAdapter
, and there I have to set which endpoints are secured and which aren't.
But my question is: do I need resource server? It do same job as my potential WebSecurityConfig
, or not?
My goal is create simple JWT authentication for my website.
Yes, you will want to configure the resources protected by your JWT's by extending
ResourceServerConfigurerAdapter
. A basic implementation might look like thisThis means you should have no need to extend
WebSecurityConfigurerAdapter
because the above configuration configures the sameHttpSecurity
object that you would be configuring inWebSecurityConfigurerAdapter
. Thepublic void configure(HttpSecurity http)
works on the same thing in both classes.The reason we want to choose
ResourceServerConfigurerAdapter
overWebSecurityConfigurerAdapter
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.
You need the resource server, because it is part of the OAuth2 spec:
Hence it is also part of Spring Security OAuth2.
The resource server configuration is more than a security configuration, see OAuth 2 Developers Guide:
You could use a Spring Security configuration (
WebSecurityConfigurerAdapter
) for other customizations for the protected resources permitted by the HttpSecurity configurer in Spring Security, but it is better to use the resource server configuration, because of:and it is the recommended way.
I will try to answer with an example: suppose you want to write a great and cool web application that can manage GMAIL accounts as well as Google-calendar data together, somehow. Apparently, your users will have to sign in with their google's credentials, so your app can get their data and manage it. Your application manages the data of the users, without getting the users' credentials.
So far so good.
In this example, the Authorization-Server is Google Accounts. The Resource Server is Google-Main and Google-Calendar (both of them) and the Client is your application.
Hope that makes sense.