- OAuth 2.0 Cookbook
- Adolfo Eloy Nascimento
- 365字
- 2021-07-08 09:35:08
How to do it...
The following steps will guide you to configure an Authorization Server and a Resource Server using Spring Security OAuth2:
- Create the initial project using Spring Initializr, as we did for other recipes in this book. Go to https://start.spring.io/ and define the following data:
- Set up the Group as com.packt.example
- Define the Artifact as implicit-server
- Add Web and Security as dependencies for this project
- After creating the implicit-server project, import it to your IDE. If you are using Eclipse, import it as a Maven project.
- Open the pom.xml file and add the following dependency as we will use the Spring Security OAuth2 project (I recommend you to use the latest version of this project, particularly if you are using JWT, which is not the case for this recipe):
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
- Open the application.properties file and add the same configuration as we did for the first recipe to set up the user's credentials (which were username adolfo and password 123).
- Create the UserProfile.java and UserController classes within the com.packt.example.implicitserver.api package. The content for both classes must be the as same provided for the first recipe (you can also download the source code from GitHub if you want).
- Now create the OAuth2ResourceServer class with the following content to declare how to protect endpoints which matches the /api/** pattern. This class should be created within the com.packt.example.implicitserver.confi package:
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and()
.requestMatchers().antMatchers("/api/**");
}
}
- Note that the Resource Server is being configured the same way we did when adding support for the Authorization Code grant type.
- Create the OAuth2AuthorizationServer class as presented in the following code, to configure the Implicit grant type:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter {
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("clientapp").secret("123456")
.redirectUris("http://localhost:9000/callback")
.authorizedGrantTypes("implicit")
.accessTokenValiditySeconds(120)
.scopes("read_profile", "read_contacts");
}
}
- The preceding class looks similar to the one created for the Authorization Code grant type. The difference now is that we are defining authorizedGrantTypes as Implicit and we are also defining a short validity time for the access token.
- Run the application using your IDE actions or by running the Maven command, mvn spring-boot:run.
推薦閱讀
- Learning Microsoft Windows Server 2012 Dynamic Access Control
- Learn TypeScript 3 by Building Web Applications
- LabVIEW 2018 虛擬儀器程序設(shè)計(jì)
- Java高手真經(jīng)(高級(jí)編程卷):Java Web高級(jí)開發(fā)技術(shù)
- Production Ready OpenStack:Recipes for Successful Environments
- 精通API架構(gòu):設(shè)計(jì)、運(yùn)維與演進(jìn)
- MATLAB實(shí)用教程
- iOS應(yīng)用逆向工程(第2版)
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- HTML5秘籍(第2版)
- C#程序設(shè)計(jì)教程(第3版)
- Learning Concurrency in Python
- 城市信息模型平臺(tái)頂層設(shè)計(jì)與實(shí)踐
- 菜鳥成長(zhǎng)之路
- Java程序設(shè)計(jì)基礎(chǔ)教程