- 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.
推薦閱讀
- C++ Primer習題集(第5版)
- Visual Basic 6.0程序設計計算機組裝與維修
- 劍指Offer(專項突破版):數據結構與算法名企面試題精講
- 程序員數學:用Python學透線性代數和微積分
- Mastering QGIS
- 動手玩轉Scratch3.0編程:人工智能科創教育指南
- Jenkins Continuous Integration Cookbook(Second Edition)
- Instant Lucene.NET
- Mastering Git
- UML2面向對象分析與設計(第2版)
- 從Excel到Python數據分析:Pandas、xlwings、openpyxl、Matplotlib的交互與應用
- Learning Nessus for Penetration Testing
- XML程序設計(第二版)
- 一覽眾山小:ASP.NET Web開發修行實錄
- Thymeleaf 3完全手冊