forked from apache/syncope
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust Spring Security settings to work around spring-projects/spring…
- Loading branch information
Showing
8 changed files
with
163 additions
and
9 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
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
111 changes: 111 additions & 0 deletions
111
.../src/main/java/org/apache/syncope/wa/starter/security/WAWebSecurityConfigurerAdapter.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,111 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.syncope.wa.starter.security; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import org.apache.syncope.common.lib.types.IdRepoEntitlement; | ||
import org.apache.syncope.wa.bootstrap.WAProperties; | ||
import org.apereo.cas.configuration.CasConfigurationProperties; | ||
import org.apereo.cas.web.CasWebSecurityConfigurer; | ||
import org.apereo.cas.web.security.CasWebSecurityConfigurerAdapter; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; | ||
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; | ||
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints; | ||
import org.springframework.boot.autoconfigure.security.SecurityProperties; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; | ||
import org.springframework.security.web.context.SecurityContextRepository; | ||
|
||
public class WAWebSecurityConfigurerAdapter extends CasWebSecurityConfigurerAdapter { | ||
|
||
protected final WAProperties waProperties; | ||
|
||
@SuppressWarnings("rawtypes") | ||
public WAWebSecurityConfigurerAdapter( | ||
final WAProperties waProperties, | ||
final CasConfigurationProperties casProperties, | ||
final SecurityProperties securityProperties, | ||
final WebEndpointProperties webEndpointProperties, | ||
final ObjectProvider<PathMappedEndpoints> pathMappedEndpoints, | ||
final List<CasWebSecurityConfigurer> protocolEndpointWebSecurityConfigurers, | ||
final SecurityContextRepository securityContextRepository) { | ||
|
||
super(casProperties, securityProperties, webEndpointProperties, pathMappedEndpoints, | ||
protocolEndpointWebSecurityConfigurers, securityContextRepository); | ||
this.waProperties = waProperties; | ||
} | ||
|
||
@Override | ||
protected void configureEndpointAccessAuthenticated( | ||
final HttpSecurity http, | ||
final EndpointRequest.EndpointRequestMatcher endpoint) throws Exception { | ||
|
||
http.authorizeHttpRequests(customizer -> customizer.requestMatchers(endpoint).authenticated()); | ||
|
||
http.with(new AbstractHttpConfigurer<HttpBasicConfigurer<HttpSecurity>, HttpSecurity>() { | ||
|
||
@Override | ||
public void configure(final HttpSecurity http) throws Exception { | ||
BasicAuthenticationFilter filter = new BasicAuthenticationFilter( | ||
http.getSharedObject(AuthenticationManager.class), | ||
new BasicAuthenticationEntryPoint()) { | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
final HttpServletRequest request, | ||
final HttpServletResponse response, | ||
final FilterChain chain) | ||
throws IOException, ServletException { | ||
|
||
if (request.getRequestURI().contains("/oidc")) { | ||
logger.trace("Skip processing since this is an OIDC request"); | ||
return; | ||
} | ||
|
||
super.doFilter(request, response, chain); | ||
} | ||
}; | ||
filter.setSecurityContextHolderStrategy(getSecurityContextHolderStrategy()); | ||
http.addFilter(postProcess(filter)); | ||
} | ||
}, Customizer.withDefaults()); | ||
|
||
UserDetails user = User.withUsername(waProperties.getAnonymousUser()). | ||
password("{noop}" + waProperties.getAnonymousKey()). | ||
roles(IdRepoEntitlement.ANONYMOUS). | ||
build(); | ||
http.userDetailsService(new InMemoryUserDetailsManager(user)); | ||
|
||
http.csrf(AbstractHttpConfigurer::disable); | ||
} | ||
} |
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