官术网_书友最值得收藏!

  • 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:

  1. 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
  1. After creating the implicit-server project, import it to your IDE. If you are using Eclipse, import it as a Maven project.
  2. 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>
  1. 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).
  2. 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).
  3. 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/**");
}
}
  1. Note that the Resource Server is being configured the same way we did when adding support for the Authorization Code grant type.
  1. 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");
}
}
  1. 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.
  2. Run the application using your IDE actions or by running the Maven command, mvn spring-boot:run.
主站蜘蛛池模板: 修武县| 江都市| 永康市| 武乡县| 安岳县| 天镇县| 辉县市| 海伦市| 安乡县| 石家庄市| 宝鸡市| 海盐县| 重庆市| 玛纳斯县| 天长市| 天峨县| 麻阳| 淮安市| 吉安县| 嘉鱼县| 凭祥市| 馆陶县| 林西县| 定兴县| 洛川县| 苍溪县| 翁源县| 汤阴县| 绍兴县| 澎湖县| 泽普县| 区。| 通渭县| 子长县| 台前县| 英吉沙县| 昌黎县| 揭西县| 齐河县| 平山县| 象州县|