Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVA-23381 GitHub Issue: Keycloak and Spring boot version update #421

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions oauth-jwt/jwt-auth-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<version>3.1.1</version>
<relativePath />
</parent>

Expand All @@ -35,12 +35,12 @@
<scope>runtime</scope>
</dependency>
<!-- Keycloak server -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-dependencies-server-all</artifactId>
<version>${keycloak.version}</version>
Expand Down Expand Up @@ -127,12 +127,12 @@
<properties>
<!-- non-dependencies -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>13</java.version>
<keycloak.version>18.0.0</keycloak.version>
<java.version>17</java.version>
<keycloak.version>22.0.0</keycloak.version>
<!-- these should be updated together with Keycloak -->
<!-- check keycloak-dependencies-server-all effective pom -->
<infinispan.version>13.0.8.Final</infinispan.version>
<resteasy.version>3.15.1.Final</resteasy.version>
<resteasy.version>6.2.4.Final</resteasy.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ FilterRegistrationBean<EmbeddedKeycloakRequestFilter> keycloakSessionManagement(
}

private void mockJndiEnvironment(DataSource dataSource) throws NamingException {
NamingManager.setInitialContextFactoryBuilder((env) -> (environment) -> new InitialContext() {
NamingManager.setInitialContextFactoryBuilder(env -> environment -> new InitialContext() {

@Override
public Object lookup(Name name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.io.UnsupportedEncodingException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;

import org.keycloak.common.ClientConnection;
import org.keycloak.services.filters.AbstractRequestFilter;
Expand All @@ -18,7 +18,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
servletRequest.setCharacterEncoding("UTF-8");
ClientConnection clientConnection = createConnection((HttpServletRequest) servletRequest);

filter(clientConnection, (session) -> {
filter(clientConnection, session -> {
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package com.baeldung.jwt.config;

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import java.util.HashMap;
import java.util.Map;

import org.jboss.resteasy.core.ResteasyContext;
import org.keycloak.common.util.ResteasyProvider;

public class Resteasy3Provider implements ResteasyProvider {

@Override
public <R> R getContextData(Class<R> type) {
ResteasyProviderFactory.getInstance();
return ResteasyProviderFactory.getContextData(type);
return ResteasyContext.getContextData(type);
}

@SuppressWarnings("rawtypes")
@Override
public void pushDefaultContextObject(Class type, Object instance) {
ResteasyProviderFactory.getInstance();
ResteasyProviderFactory.getContextData(Dispatcher.class)
.getDefaultContextObjects()
.put(type, instance);
Map<Class<?>, Object> map = new HashMap<>();
map.put(type, instance);
ResteasyContext.pushContextDataMap(map);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void pushContext(Class type, Object instance) {
ResteasyProviderFactory.getInstance();
ResteasyProviderFactory.pushContext(type, instance);
ResteasyContext.pushContext(type, instance);
}

@Override
public void clearContextData() {
ResteasyProviderFactory.getInstance();
ResteasyProviderFactory.clearContextData();
ResteasyContext.clearContextData();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import java.io.File;

import org.keycloak.Config;
import org.keycloak.platform.PlatformProvider;
import org.keycloak.services.ServicesLogger;

public class SimplePlatformProvider implements PlatformProvider {

Runnable shutdownHook;

@Override
@Override
public String name() {
return "SimplePlatformProvider";
}

@Override
public void onStartup(Runnable startupHook) {
startupHook.run();
}
Expand All @@ -26,16 +32,16 @@ public void exit(Throwable cause) {
}

private void exit(int status) {
new Thread() {
@Override
public void run() {
System.exit(status);
}
}.start();
new Thread(() -> System.exit(status)).start();
}

@Override
public File getTmpDirectory() {
return new File(System.getProperty("java.io.tmpdir"));
}

@Override
public ClassLoader getScriptEngineClassLoader(Config.Scope scope) {
return null;
}
}
6 changes: 3 additions & 3 deletions oauth-jwt/jwt-resource-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<version>3.1.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

Expand All @@ -32,7 +32,7 @@
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.5.RELEASE</version>
<version>2.5.2.RELEASE</version>
</dependency>
<!-- persistence -->
<dependency>
Expand Down Expand Up @@ -90,7 +90,7 @@
<properties>
<!-- non-dependencies -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>13</java.version>
<java.version>17</java.version>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootApplication
public class JWTResourceServerApp {

public static void main(String[] args) throws Exception {
public static void main(String[] args) {
SpringApplication.run(JWTResourceServerApp.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.baeldung.jwt.persistence.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.baeldung.jwt.persistence.repository;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.CrudRepository;

import com.baeldung.jwt.persistence.model.Foo;

public interface IFooRepository extends PagingAndSortingRepository<Foo, Long> {
public interface IFooRepository extends CrudRepository<Foo, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package com.baeldung.jwt.spring;

import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
public class SecurityConfig {

@Override
protected void configure(HttpSecurity http) throws Exception {// @formatter:off
http.cors()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/user/info", "/api/foos/**")
.hasAuthority("SCOPE_read")
.antMatchers(HttpMethod.POST, "/api/foos")
.hasAuthority("SCOPE_write")
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer()
.jwt();
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
private String issuerUri;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {// @formatter:off
http.cors(httpSecurityCorsConfigurer -> httpSecurityCorsConfigurer.configure(http))
.authorizeHttpRequests(authorizeRequest ->
authorizeRequest.requestMatchers(HttpMethod.GET, "/user/info", "/api/foos/**").hasAuthority("SCOPE_read")
.requestMatchers(HttpMethod.POST, "/api/foos").hasAuthority("SCOPE_write")
.anyRequest().authenticated())
.oauth2ResourceServer(oauth2ResourceServer -> oauth2ResourceServer.jwt(jwtConfigurer -> jwtConfigurer.decoder(jwtDecoder())));

return http.build();
}// @formatter:on

@Bean
JwtDecoder jwtDecoder(OAuth2ResourceServerProperties properties) {
NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(properties.getJwt().getJwkSetUri()).build();
JwtDecoder jwtDecoder() {
NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(issuerUri).build();
jwtDecoder.setClaimSetConverter(new OrganizationSubClaimAdapter());
return jwtDecoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.baeldung.jwt.JWTResourceServerApp;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { JWTResourceServerApp.class })
public class ContextIntegrationTest {
class ContextIntegrationTest {

@Test
public void whenLoadApplication_thenSuccess() {
void whenLoadApplication_thenSuccess() {

}

Expand Down
Loading