Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c2b54d

Browse files
add refresh token
1 parent f6f0a81 commit 7c2b54d

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

‎readme.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ curl spring-rest-oauth2-jwt:B6813193F1D7EC8BF5B40@localhost:8080/oauth/token -d
3939

4040
```json
4141
{
42-
"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiREEyRDUzMkQxQkVqd3RyZXNvdXJjZWlkIl0sInVzZXJfbmFtZSI6ImdhYnJpZWxjemFyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTUyNzI2MjI5MiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6ImQ5MTI4NmFmLWMwYjEtNDQ1Ni1hOTVkLTcwYjMyYzVmM2E5ZCIsImNsaWVudF9pZCI6IjY2OTdhMTA1MzMxYzkxMTczYTc2MzgxZWJkMjQ5Mjc4In0.wNFb3iXdcXavNCjSSzorWVwFg27n0eebRS1XrT3Ans8",
42+
"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiREEyRDUzMkQxQkVqd3RyZXNvdXJjZWlkIl0sInVzZXJfbmFtZSI6ImdhYnJpZWxjemFyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTcyNzMwNTQ3OCwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6ImY4MTJkYzBlLWZiZmItNDQ1My04ZDI2LTY5NmM2NWQ0MzA5ZiIsImNsaWVudF9pZCI6InNwcmluZy1yZXN0LW9hdXRoMi1qd3QifQ.ExobK5qYHzSxVpoPUvT8uQwBfZwsefYWsEjsxJopni0",
4343
"token_type":"bearer",
44-
"expires_in":43199,
44+
"refresh_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiREEyRDUzMkQxQkVqd3RyZXNvdXJjZWlkIl0sInVzZXJfbmFtZSI6ImdhYnJpZWxjemFyIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImF0aSI6ImY4MTJkYzBlLWZiZmItNDQ1My04ZDI2LTY5NmM2NWQ0MzA5ZiIsImV4cCI6MTUyOTg5NjQ5MCwiYXV0aG9yaXRpZXMiOlsiUk9MRV9VU0VSIl0sImp0aSI6IjZlNWMyNDRhLTU5NDQtNDNhMy05ZjJlLTkwMWQ1ZDFkNTJjYiIsImNsaWVudF9pZCI6InNwcmluZy1yZXN0LW9hdXRoMi1qd3QifQ.E3w8BWVdtFyw6eBwBokXIXRtE3-oxdwH0JgLf-13wfo",
45+
"expires_in":604800,
4546
"scope":"read write",
46-
"jti":"d91286af-c0b1-4456-a95d-70b32c5f3a9d"
47-
}
47+
"jti":"f812dc0e-fbfb-4453-8d26-696c65d4309f"
48+
}
4849
```
4950

5051
- Acessar recurso:

‎src/main/java/com/gabrielczar/springrestoauth2jwt/configurations/AuthorizationServerConfiguration.java‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.springframework.security.oauth2.provider.token.TokenStore;
1313
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
1414

15-
import java.util.Arrays;
1615
import java.util.Collections;
1716

1817
@Configuration
@@ -31,6 +30,12 @@ public class AuthorizationServerConfiguration extends AuthorizationServerConfigu
3130
@Value("${security.jwt.grant-type}")
3231
private String grantType;
3332

33+
@Value("${security.jwt.grant-type-auth-code}")
34+
private String grantTypeAuthCode;
35+
36+
@Value("${security.jwt.grant-type-refresh-token}")
37+
private String grantTypeRefreshToken;
38+
3439
@Value("${security.jwt.scope-read}")
3540
private String scopeRead;
3641

@@ -40,6 +45,12 @@ public class AuthorizationServerConfiguration extends AuthorizationServerConfigu
4045
@Value("${security.jwt.resource-ids}")
4146
private String resourceIds;
4247

48+
@Value("${security.jwt.access-token-validity}")
49+
private Integer accessTokenValidity;
50+
51+
@Value("${security.jwt.refresh-token-validity}")
52+
private Integer refreshTokenValidity;
53+
4354
@Autowired
4455
public AuthorizationServerConfiguration(TokenStore tokenStore, JwtAccessTokenConverter accessTokenConverter, AuthenticationManager authenticationManager) {
4556
this.tokenStore = tokenStore;
@@ -53,9 +64,11 @@ public void configure(ClientDetailsServiceConfigurer configurer) throws Exceptio
5364
.inMemory()
5465
.withClient(clientId)
5566
.secret(clientSecret)
56-
.authorizedGrantTypes(grantType)
67+
.authorizedGrantTypes(grantType, grantTypeAuthCode, grantTypeRefreshToken)
5768
.scopes(scopeRead, scopeWrite)
58-
.resourceIds(resourceIds);
69+
.resourceIds(resourceIds)
70+
.refreshTokenValiditySeconds(refreshTokenValidity)
71+
.accessTokenValiditySeconds(accessTokenValidity);
5972
}
6073

6174
@Override

‎src/main/java/com/gabrielczar/springrestoauth2jwt/configurations/ResourceServerConfiguration.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.beans.factory.annotation.Value;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.HttpMethod;
67
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
78
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
89
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
@@ -30,12 +31,12 @@ public void configure(ResourceServerSecurityConfigurer resources) throws Excepti
3031
@Override
3132
public void configure(HttpSecurity http) throws Exception {
3233
http
33-
.requestMatchers()
34-
.and()
34+
.logout()
35+
.invalidateHttpSession(true)
36+
.clearAuthentication(true).and()
3537
.authorizeRequests()
36-
.antMatchers("/actuator/**", "/api-docs/**")
37-
.permitAll()
38-
.antMatchers("/api/**" )
39-
.authenticated();
38+
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
39+
.antMatchers("/actuator/**", "/api-docs/**").permitAll()
40+
.antMatchers("/api/**" ).authenticated();
4041
}
4142
}

‎src/main/java/com/gabrielczar/springrestoauth2jwt/configurations/SecurityConfiguration.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import org.springframework.context.annotation.Configuration;
77
import org.springframework.context.annotation.Primary;
88
import org.springframework.security.authentication.AuthenticationManager;
9-
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
109
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1110
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1211
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
13-
import org.springframework.security.config.annotation.web.builders.WebSecurity;
1412
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1513
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1614
import org.springframework.security.config.http.SessionCreationPolicy;
@@ -84,7 +82,6 @@ public TokenStore tokenStore() {
8482

8583
@Bean
8684
@Primary
87-
//Making this primary to avoid any accidental duplication with another token service instance of the same name
8885
public DefaultTokenServices tokenServices() {
8986
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
9087
defaultTokenServices.setTokenStore(tokenStore());

‎src/main/resources/application.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ security:
1919
client-id: spring-rest-oauth2-jwt
2020
client-secret: B6813193F1D7EC8BF5B40
2121
grant-type: password
22+
grant-type-auth-code: authorization_code
23+
grant-type-refresh-token: refresh_token
2224
scope-read: read
2325
scope-write: write
24-
resource-ids: DA2D532D1BEjwtresourceid
26+
resource-ids: DA2D532D1BEjwtresourceid
27+
access-token-validity: 604800 # 7 days
28+
refresh-token-validity: 2592000 # 30 days

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /