This is the code snippet of my authorization server which is running on spring boot 2.2.4 and running on port 7220. I can get the access token by calling this API call http://localhost:7220/oauth/token
<?xml>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<description>my authentication Service</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@SpringBootApplication
@EnableResourceServer
@EnableDiscoveryClient
@EnableFeignClients
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyAuthenticationServiceApplication {
public static void main(String[] args) {
SpringApplication.run(WellnessAuthenticationServiceApplication.class, args);
}
}
@EnableAuthorizationServer
@Slf4j
@Configuration
@RequiredArgsConstructor
class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private final GeneralUtil generalUtil;
private final AuthenticationManager authenticationManager;
private final UserRepository userRepository;
private final UserDetailsService userDetailsService;
private final PasswordEncoder passwordEncoder;
private final TokenStore tokenStore;
@Value("${user.oauth.client}")
private String client;
@Value("${user.oauth.secret}")
private String secret;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
return new JwtAccessTokenConverter() {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
userRepository.findById(((org.springframework.security.core.userdetails.User) oAuth2Authentication.getPrincipal()).getUsername())
.ifPresent(user ->
((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(Map.of(
"firstName", user.getFirstName(),
"lastName", user.getLastName(),
"email", user.getEmail(),
"phoneNumber", user.getPhoneNumber(),
"configured", user.isConfigured())));
return super.enhance(oAuth2AccessToken, oAuth2Authentication);
}
};
}
@Bean
public WebResponseExceptionTranslator<OAuth2Exception> loggingExceptionTranslator() {
return new DefaultWebResponseExceptionTranslator() {
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
return super.translate(e);
}
};
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("permitAll()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient(client).secret(passwordEncoder.encode(secret)).authorizedGrantTypes("password", "refresh_token").scopes("openid").accessTokenValiditySeconds(60000).refreshTokenValiditySeconds(60000);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(this.authenticationManager).accessTokenConverter(jwtAccessTokenConverter());
endpoints.userDetailsService(userDetailsService);
endpoints.exceptionTranslator(loggingExceptionTranslator());
endpoints.tokenStore(this.tokenStore);
endpoints.reuseRefreshTokens(false);
}
}
Below is the code snippet of my resource server which is running on spring boot 3.2.1.
<?xml>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.my</groupId>
<artifactId>my-dashboard-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>my-dashboard-gateway</name>
<description>my dashboard Gateway</description>m
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!--version>1.18.30</version-->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.application.name=my-dashboard-gateway
server.port=7240
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:7220
This is the endpoint that i am trying to access which is defined my-dashboard-gateway(resource server).
@GetMapping("/companies")
public ResponseEntity<String> get() {
return ResponseEntity.ok().body("Hello this is my company");
}
This is the exception occurred in on my resource server.
This is the exception occurred in authentication server.
Problem is Resource server is running on a new version and auth server is running on a old version.How do we validate the token coming to the resource server. what sort of configurations we need. Is there anything we have to do with following two end points
spring.security.oauth2.resourceserver.jwt.issuer-uri
spring.security.oauth2.resourceserver.jwt.key-set-uri