Skip to content

Commit 8ed5156

Browse files
committed
Final Project Base version 2.0.1
1 parent 05514c3 commit 8ed5156

File tree

8 files changed

+77
-14
lines changed

8 files changed

+77
-14
lines changed

.DS_Store

8 KB
Binary file not shown.

src/main/java/com/oauth2/config/auth/OAuthConfiguration.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.oauth2.config.auth;
22

3+
import java.util.Arrays;
4+
35
import org.springframework.beans.factory.annotation.Value;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
@@ -10,7 +12,13 @@
1012
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
1113
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
1214
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
15+
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
16+
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
17+
import org.springframework.security.oauth2.provider.token.TokenStore;
1318
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
19+
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
20+
21+
import com.oauth2.config.auth.token.CustomTokenEnhancer;
1422

1523
@Configuration
1624
@EnableAuthorizationServer
@@ -37,6 +45,7 @@ public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {
3745
@Value("${jwt.refreshTokenValiditySeconds}")
3846
private int refreshTokenValiditySeconds;//30days
3947

48+
4049
public OAuthConfiguration(AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder, UserDetailsService userService) {
4150
this.authenticationManager = authenticationManager;
4251
this.passwordEncoder = passwordEncoder;
@@ -51,22 +60,36 @@ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
5160
.accessTokenValiditySeconds(accessTokenValiditySeconds)
5261
.refreshTokenValiditySeconds(refreshTokenValiditySeconds)
5362
.authorizedGrantTypes(authorizedGrantTypes)
54-
.scopes("read", "write")
55-
.resourceIds("api");
63+
.scopes("read", "write");
5664
}
5765

5866
@Override
5967
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
60-
endpoints
61-
.accessTokenConverter(accessTokenConverter())
68+
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
69+
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhacer(), accessTokenConverter()));
70+
71+
endpoints
72+
.tokenStore(tokenStore())
73+
.tokenEnhancer(tokenEnhancerChain)
74+
.reuseRefreshTokens(false)
6275
.userDetailsService(userService)
6376
.authenticationManager(authenticationManager);
6477
}
6578

6679
@Bean
6780
JwtAccessTokenConverter accessTokenConverter() {
68-
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
69-
return converter;
81+
JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
82+
accessTokenConverter.setSigningKey("maracuja");
83+
return accessTokenConverter;
7084
}
85+
86+
@Bean
87+
public TokenStore tokenStore(){
88+
return new JwtTokenStore(accessTokenConverter());
89+
}
90+
91+
public TokenEnhancer tokenEnhacer() {
92+
return new CustomTokenEnhancer();
93+
}
7194

7295
}

src/main/java/com/oauth2/config/auth/UserPrincipal.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public String getPassword() {
3636
return user.getPassword();
3737
}
3838

39-
//UUID -> User
4039
@Override
4140
public String getUsername() {
42-
return user.getUuid().toString();
41+
return user.getEmail();
4342
}
4443

4544
@Override
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.oauth2.config.auth.token;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
7+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
8+
import org.springframework.security.oauth2.provider.OAuth2Authentication;
9+
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
10+
11+
import com.oauth2.config.auth.UserPrincipal;
12+
13+
public class CustomTokenEnhancer implements TokenEnhancer{
14+
15+
@Override
16+
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
17+
18+
UserPrincipal userAuth = (UserPrincipal) authentication.getPrincipal();
19+
20+
Map<String, Object> addInfo = new HashMap<>();
21+
addInfo.put("user_uuid", userAuth.getUser().getUuid());
22+
23+
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(addInfo);
24+
return accessToken;
25+
}
26+
27+
}

src/main/java/com/oauth2/controllers/AuthController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.springframework.web.bind.annotation.RestController;
1515

1616
import com.oauth2.entities.User;
17-
import com.oauth2.models.dto.auth.AuthUserRoleAndAuthoritiesDTO;
17+
import com.oauth2.models.dto.auth.AuthUserAndRolesAndAuthoritiesDTO;
1818
import com.oauth2.services.IUserService;
1919

2020
import io.swagger.annotations.Api;
@@ -30,14 +30,14 @@ public class AuthController {
3030
private IUserService userService;
3131

3232
@GetMapping(value = "/authorities/{uuid}", produces = MediaType.APPLICATION_JSON_VALUE)
33-
public ResponseEntity<AuthUserRoleAndAuthoritiesDTO> getAuthorities(@PathVariable String uuid){
33+
public ResponseEntity<AuthUserAndRolesAndAuthoritiesDTO> getAuthorities(@PathVariable String uuid){
3434
try {
3535
UUID uuid_user = UUID.fromString(uuid.toString());
3636

3737
User user = userService.findByUuid(uuid_user)
3838
.orElseThrow(() -> new UsernameNotFoundException("Error -> hasPermission for UUID: " + uuid_user));
3939

40-
return ResponseEntity.ok(new AuthUserRoleAndAuthoritiesDTO(user));
40+
return ResponseEntity.ok(new AuthUserAndRolesAndAuthoritiesDTO(user));
4141
} catch (IllegalArgumentException ie) {
4242
log.error("Error method getAuthorities in class AuthController: "+ie.getMessage());
4343
return ResponseEntity.badRequest().build();//400

src/main/java/com/oauth2/models/dto/auth/AuthUserRoleAndAuthoritiesDTO.java renamed to src/main/java/com/oauth2/models/dto/auth/AuthUserAndRolesAndAuthoritiesDTO.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
@AllArgsConstructor
1414
@NoArgsConstructor
1515
@Data
16-
public class AuthUserRoleAndAuthoritiesDTO {
16+
public class AuthUserAndRolesAndAuthoritiesDTO {
1717

1818
private String uuid;
19+
private String name;
20+
private String email;
1921
private List<AuthRolesDTO> roles;
2022

21-
public AuthUserRoleAndAuthoritiesDTO(User user) {
23+
public AuthUserAndRolesAndAuthoritiesDTO(User user) {
2224
this.uuid = user.getUuid().toString();
25+
this.name = user.getName();
26+
this.email = user.getEmail();
2327
this.roles = new ArrayList<>();
2428
this.roles.addAll(user.getRoles().stream()
2529
.map(AuthRolesDTO::new)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.oauth2.utils;
2+
3+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
4+
5+
public class GeneratorPassword {
6+
public static void main(String[] args) {
7+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
8+
System.out.println(encoder.encode("secretProjectExampleOAuth2Security"));
9+
}
10+
}

src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jwt:
4242
clientId: ProjectExampleOAuth2Security
4343
client-secret: secretProjectExampleOAuth2Security
4444
accessTokenValidititySeconds: 43200
45-
authorizedGrantTypes: password,authorization_code,refresh_token
45+
authorizedGrantTypes: password,refresh_token
4646
refreshTokenValiditySeconds: 2592000
4747

4848
server:

0 commit comments

Comments
 (0)