forked from jobmission/oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/revengemission/sso/oauth2/server/config/JwkSetEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.revengemission.sso.oauth2.server.config; | ||
|
||
import com.nimbusds.jose.jwk.JWKSet; | ||
import com.nimbusds.jose.jwk.RSAKey; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint; | ||
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.security.KeyPair; | ||
import java.security.Principal; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.util.Map; | ||
|
||
@FrameworkEndpoint | ||
class JwkSetEndpoint { | ||
|
||
@Value("${jwt.jks.keypass:keypass}") | ||
private String keypass; | ||
|
||
KeyPair keyPair; | ||
|
||
@PostConstruct | ||
public void initData() { | ||
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("jwt.jks"), keypass.toCharArray()); | ||
this.keyPair = keyStoreKeyFactory.getKeyPair("jwt"); | ||
} | ||
|
||
@GetMapping("/.well-known/jwks.json") | ||
@ResponseBody | ||
public Map<String, Object> getKey(Principal principal) { | ||
RSAPublicKey publicKey = (RSAPublicKey) this.keyPair.getPublic(); | ||
RSAKey key = new RSAKey.Builder(publicKey).build(); | ||
return new JWKSet(key).toJSONObject(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/revengemission/sso/oauth2/server/config/JwkSetEndpointConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.revengemission.sso.oauth2.server.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration; | ||
|
||
@Configuration | ||
class JwkSetEndpointConfiguration extends AuthorizationServerSecurityConfiguration { | ||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
super.configure(http); | ||
http | ||
.requestMatchers() | ||
.mvcMatchers("/.well-known/jwks.json") | ||
.and() | ||
.authorizeRequests() | ||
.mvcMatchers("/.well-known/jwks.json").permitAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters