11package com .chensoul .config ;
22
3+ import com .chensoul .support .AccessTokenResponseHandler ;
4+ import com .chensoul .support .FederatedIdentityAuthenticationSuccessHandler ;
35import org .springframework .boot .autoconfigure .security .oauth2 .server .servlet .OAuth2AuthorizationServerAutoConfiguration ;
46import org .springframework .boot .autoconfigure .security .oauth2 .server .servlet .OAuth2AuthorizationServerJwtAutoConfiguration ;
57import org .springframework .context .annotation .Bean ;
68import org .springframework .context .annotation .Configuration ;
9+ import org .springframework .core .Ordered ;
710import org .springframework .core .annotation .Order ;
811import org .springframework .http .MediaType ;
912import org .springframework .security .config .Customizer ;
1013import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
11- import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1214import org .springframework .security .core .authority .AuthorityUtils ;
15+ import org .springframework .security .core .session .SessionRegistry ;
16+ import org .springframework .security .core .session .SessionRegistryImpl ;
17+ import org .springframework .security .core .userdetails .User ;
18+ import org .springframework .security .core .userdetails .UserDetails ;
19+ import org .springframework .security .core .userdetails .UserDetailsService ;
1320import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
1421import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
1522import org .springframework .security .oauth2 .core .oidc .OidcScopes ;
16- import org .springframework .security .oauth2 .server .authorization .OAuth2TokenType ;
23+ import org .springframework .security .oauth2 .server .authorization .* ;
1724import org .springframework .security .oauth2 .server .authorization .client .InMemoryRegisteredClientRepository ;
1825import org .springframework .security .oauth2 .server .authorization .client .RegisteredClient ;
1926import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
2027import org .springframework .security .oauth2 .server .authorization .config .annotation .web .configurers .OAuth2AuthorizationServerConfigurer ;
28+ import org .springframework .security .oauth2 .server .authorization .settings .AuthorizationServerSettings ;
2129import org .springframework .security .oauth2 .server .authorization .settings .ClientSettings ;
2230import org .springframework .security .oauth2 .server .authorization .settings .OAuth2TokenFormat ;
2331import org .springframework .security .oauth2 .server .authorization .settings .TokenSettings ;
2432import org .springframework .security .oauth2 .server .authorization .token .JwtEncodingContext ;
2533import org .springframework .security .oauth2 .server .authorization .token .OAuth2TokenCustomizer ;
34+ import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
2635import org .springframework .security .web .SecurityFilterChain ;
36+ import org .springframework .security .web .authentication .AuthenticationSuccessHandler ;
2737import org .springframework .security .web .authentication .LoginUrlAuthenticationEntryPoint ;
38+ import org .springframework .security .web .session .HttpSessionEventPublisher ;
2839import org .springframework .security .web .util .matcher .MediaTypeRequestMatcher ;
2940
3041import java .util .Set ;
3647 * @see OAuth2AuthorizationServerAutoConfiguration
3748 * @see OAuth2AuthorizationServerJwtAutoConfiguration
3849 */
39- @ EnableWebSecurity (debug = true )
4050@ Configuration
4151public class SecurityConfig {
52+ private static final String CUSTOM_CONSENT_PAGE_URI = "/oauth2/consent" ;
53+
4254 @ Bean
43- @ Order (1 )
55+ @ Order (Ordered . HIGHEST_PRECEDENCE )
4456 SecurityFilterChain authorizationServerSecurityFilterChain (HttpSecurity http ) throws Exception {
4557 OAuth2AuthorizationServerConfigurer authorizationServerConfigurer = authorizationServer ();
4658
4759 http .securityMatcher (authorizationServerConfigurer .getEndpointsMatcher ())
4860 .with (authorizationServerConfigurer , (authorizationServer ) ->
4961 authorizationServer
62+ .authorizationEndpoint (authorizationEndpoint ->
63+ authorizationEndpoint .consentPage (CUSTOM_CONSENT_PAGE_URI ))
5064 .tokenEndpoint (token ->
5165 token .accessTokenResponseHandler (new AccessTokenResponseHandler ()))
5266 .oidc (Customizer .withDefaults ()) // Enable OpenID Connect 1.0
@@ -58,14 +72,47 @@ SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) th
5872 new LoginUrlAuthenticationEntryPoint ("/login" ),
5973 new MediaTypeRequestMatcher (MediaType .TEXT_HTML )
6074 )
75+ );
76+
77+ return http .build ();
78+ }
79+
80+ @ Bean
81+ public SecurityFilterChain defaultSecurityFilterChain (HttpSecurity http ) throws Exception {
82+ http
83+ .authorizeHttpRequests (authorize ->
84+ authorize
85+ .requestMatchers ("/webjars/**" , "/assets/**" , "/login" , "/logged-out" ).permitAll ()
86+ .anyRequest ().authenticated ()
87+ )
88+ .formLogin (formLogin ->
89+ formLogin
90+ .loginPage ("/login" )
6191 )
62- // Accept access tokens for User Info and/or Client Registration
63- .oauth2ResourceServer ((resourceServer ) -> resourceServer
64- .jwt (Customizer .withDefaults ()));
92+ .oauth2Login (oauth2Login ->
93+ oauth2Login
94+ .loginPage ("/login" )
95+ .successHandler (authenticationSuccessHandler ())
96+ );
6597
6698 return http .build ();
6799 }
68100
101+ private AuthenticationSuccessHandler authenticationSuccessHandler () {
102+ return new FederatedIdentityAuthenticationSuccessHandler ();
103+ }
104+
105+ // @formatter:off
106+ @ Bean
107+ public UserDetailsService users () {
108+ UserDetails user = User .withDefaultPasswordEncoder ()
109+ .username ("user" )
110+ .password ("password" )
111+ .roles ("USER" )
112+ .build ();
113+ return new InMemoryUserDetailsManager (user );
114+ }
115+
69116 @ Bean
70117 public RegisteredClientRepository registeredClientRepository () {
71118 // @formatter:off
@@ -140,12 +187,28 @@ public RegisteredClientRepository registeredClientRepository() {
140187 }
141188
142189 @ Bean
143- public OAuth2TokenCustomizer <JwtEncodingContext > jwtTokenCustomizer () {
144- return (context ) -> {
145- if (context .getTokenType ().equals (OAuth2TokenType .ACCESS_TOKEN )) {
146- Set <String > authorities = AuthorityUtils .authorityListToSet (context .getPrincipal ().getAuthorities ());
147- context .getClaims ().claim ("authorities" , authorities );
148- }
149- };
190+ public AuthorizationServerSettings authorizationServerSettings () {
191+ return AuthorizationServerSettings .builder ().build ();
192+ }
193+
194+ @ Bean
195+ public OAuth2AuthorizationService authorizationService () {
196+ return new InMemoryOAuth2AuthorizationService ();
197+ }
198+
199+ @ Bean
200+ public OAuth2AuthorizationConsentService authorizationConsentService () {
201+ return new InMemoryOAuth2AuthorizationConsentService ();
202+ }
203+
204+
205+ @ Bean
206+ public SessionRegistry sessionRegistry () {
207+ return new SessionRegistryImpl ();
208+ }
209+
210+ @ Bean
211+ public HttpSessionEventPublisher httpSessionEventPublisher () {
212+ return new HttpSessionEventPublisher ();
150213 }
151214}
0 commit comments