diff --git a/pom.xml b/pom.xml index f5d485b08..e819890b3 100644 --- a/pom.xml +++ b/pom.xml @@ -23,59 +23,57 @@ UTF-8 1.8 9081 - 127.0.0.1 - readme - 1.18.10 + 127.0.0.1 + readme + 17 + 17 + 1.18.36 org.springframework.boot spring-boot-starter-actuator + 3.4.0 org.springframework.boot spring-boot-starter-data-jpa + 3.4.0 org.springframework.boot spring-boot-starter-security + 3.4.0 org.springframework.boot spring-boot-starter-web + 3.2.0 org.springframework.boot spring-boot-devtools + 3.4.0 runtime com.h2database h2 + 2.3.232 runtime org.springframework.boot spring-boot-starter-test + 3.2.2 test - - org.springframework.security - spring-security-jwt - 1.0.7.RELEASE - - - - org.springframework.security.oauth - spring-security-oauth2 - 2.1.0.RELEASE - com.fasterxml.jackson.core jackson-databind - 2.9.10.8 + 2.18.2 @@ -91,6 +89,7 @@ org.springframework.boot spring-boot-maven-plugin + 3.4.0 true true @@ -107,6 +106,7 @@ io.fabric8 docker-maven-plugin + 0.45.1 true @@ -142,11 +142,11 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.13.0 1.8 - 1.8 - + 17 + org.projectlombok lombok @@ -158,4 +158,4 @@ - + \ No newline at end of file diff --git a/src/main/java/com/nouhoun/springboot/jwt/integration/config/AuthorizationServerConfig.java b/src/main/java/com/nouhoun/springboot/jwt/integration/config/AuthorizationServerConfig.java index bf2de94fb..fafd87a52 100644 --- a/src/main/java/com/nouhoun/springboot/jwt/integration/config/AuthorizationServerConfig.java +++ b/src/main/java/com/nouhoun/springboot/jwt/integration/config/AuthorizationServerConfig.java @@ -1,7 +1,7 @@ package com.nouhoun.springboot.jwt.integration.config; import java.util.Arrays; - +import java.util.Collections; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @@ -12,24 +12,32 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenEnhancerChain; -import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; +import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; +import org.springframework.context.annotation.Bean; + +import com.nouhoun.springboot.jwt.integration.config.CustomUserDetailsService; /** * Created by nydiarra on 06/05/17. - */ + *

@Configuration -@EnableAuthorizationServer + + public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Value("${security.jwt.client-id}") private String clientId; @Value("${security.jwt.client-secret}") - private String clientSecret; + private String clientSecret = "password"; @Value("${security.jwt.grant-type}") + private String grantType = "password"; private String grantType; @Value("${security.jwt.scope-read}") @@ -41,15 +49,17 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap @Value("${security.jwt.resource-ids}") private String resourceIds; - @Autowired - private TokenStore tokenStore; + @Autowired - private JwtAccessTokenConverter accessTokenConverter; + private CustomUserDetailsService userDetailsService; @Autowired private AuthenticationManager authenticationManager; + @Autowired + private TokenStore tokenStore; + @Autowired private PasswordEncoder passwordEncoder; @@ -57,21 +67,41 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap public void configure(ClientDetailsServiceConfigurer configurer) throws Exception { configurer .inMemory() - .withClient(clientId) - .secret(passwordEncoder.encode(clientSecret)) + .withClient(clientId) + .secret(passwordEncoder.encode(clientSecret)) .authorizedGrantTypes(grantType) - .scopes(scopeRead, scopeWrite) - .resourceIds(resourceIds); + .scopes(scopeRead, scopeWrite); +// .resourceIds(resourceIds); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { TokenEnhancerChain enhancerChain = new TokenEnhancerChain(); - enhancerChain.setTokenEnhancers(Arrays.asList(accessTokenConverter)); endpoints.tokenStore(tokenStore) - .accessTokenConverter(accessTokenConverter) + .accessTokenConverter(jwtAccessTokenConverter()) .tokenEnhancer(enhancerChain) - .authenticationManager(authenticationManager); + .userDetailsService(userDetailsService) + .authenticationManager(authenticationManager()) + .pathMapping("/oauth/token", "/api/oauth/token"); } -} + +// @Override +// public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { +// security.tokenKeyAccess("isAuthenticated()") +// .checkTokenAccess("isAuthenticated()"); +// } + +// @Bean +// public AuthenticationManager authenticationManager() { +// return new AuthenticationManager(); +// } + @Bean + public JwtAccessTokenConverter jwtAccessTokenConverter() { + JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); + converter.setSigningKey("1234567890abcdef"); + return converter; + DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter(); + converter.setUserTokenConverter(new UserAuthenticationConverter()); + return converter; + } \ No newline at end of file diff --git a/src/main/java/com/nouhoun/springboot/jwt/integration/config/SecurityConfig.java b/src/main/java/com/nouhoun/springboot/jwt/integration/config/SecurityConfig.java index 8d71cb221..d9fa97e5c 100644 --- a/src/main/java/com/nouhoun/springboot/jwt/integration/config/SecurityConfig.java +++ b/src/main/java/com/nouhoun/springboot/jwt/integration/config/SecurityConfig.java @@ -60,8 +60,8 @@ protected void configure(HttpSecurity http) throws Exception { @Bean public JwtAccessTokenConverter accessTokenConverter() { - JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setSigningKey(signingKey); + JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); return converter; } @@ -78,4 +78,4 @@ public DefaultTokenServices tokenServices() { defaultTokenServices.setSupportRefreshToken(true); return defaultTokenServices; } -} +} \ No newline at end of file diff --git a/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/AppUserDetailsService.java b/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/AppUserDetailsService.java index 5592acc2a..d2603d9e0 100644 --- a/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/AppUserDetailsService.java +++ b/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/AppUserDetailsService.java @@ -39,4 +39,4 @@ public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException return userDetails; } -} +} \ No newline at end of file diff --git a/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/GenericServiceImpl.java b/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/GenericServiceImpl.java index 0af2e35d9..833e7e79c 100644 --- a/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/GenericServiceImpl.java +++ b/src/main/java/com/nouhoun/springboot/jwt/integration/service/impl/GenericServiceImpl.java @@ -28,11 +28,11 @@ public User findByUsername(String username) { @Override public List findAllUsers() { - return (List)userRepository.findAll(); + return userRepository.findAll() instanceof List ? (List) userRepository.findAll() : null; } @Override public List findAllRandomCities() { - return (List)randomCityRepository.findAll(); + return randomCityRepository.findAll() instanceof List ? (List) randomCityRepository.findAll() : null; } -} +} \ No newline at end of file