Last active
November 23, 2021 19:46
-
-
Save sjohnr/217def054d6056e83d938632bbfd023d to your computer and use it in GitHub Desktop.
Siteminder Security Example
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
/* | |
* Copyright 2002-2021 the original author or authors. | |
* | |
* Licensed 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 | |
* | |
* https://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. | |
*/ | |
import org.springframework.security.core.annotation.AuthenticationPrincipal; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.Map; | |
@RestController | |
public class HomeController { | |
@GetMapping("/") | |
public Map<String, String> home(@AuthenticationPrincipal User user) { | |
return Map.of("message", "You are logged in, " + user.getUsername() + "!"); | |
} | |
} |
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
/* | |
* Copyright 2002-2021 the original author or authors. | |
* | |
* Licensed 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 | |
* | |
* https://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. | |
*/ | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.security.authentication.AuthenticationProvider; | |
import org.springframework.security.authentication.ProviderManager; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | |
import org.springframework.security.web.SecurityFilterChain; | |
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider; | |
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter; | |
/** | |
* @author Steve Riesenberg | |
*/ | |
@EnableWebSecurity | |
public class SecurityConfiguration { | |
@Bean | |
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | |
// @formatter:off | |
http | |
.authorizeHttpRequests(authorize -> authorize | |
.anyRequest().authenticated() | |
) | |
.addFilterAfter(siteminderFilter(), RequestHeaderAuthenticationFilter.class); | |
// @formatter:on | |
return http.build(); | |
} | |
private RequestHeaderAuthenticationFilter siteminderFilter() { | |
RequestHeaderAuthenticationFilter authenticationFilter = new RequestHeaderAuthenticationFilter(); | |
authenticationFilter.setPrincipalRequestHeader("SM_USER"); | |
authenticationFilter.setExceptionIfHeaderMissing(false); | |
authenticationFilter.setAuthenticationManager(new ProviderManager(siteminderProvider())); | |
return authenticationFilter; | |
} | |
private AuthenticationProvider siteminderProvider() { | |
PreAuthenticatedAuthenticationProvider authenticationProvider = new PreAuthenticatedAuthenticationProvider(); | |
authenticationProvider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<>(userDetailsService())); | |
return authenticationProvider; | |
} | |
private UserDetailsService userDetailsService() { | |
// @formatter:off | |
UserDetails userDetails = User.withDefaultPasswordEncoder() | |
.username("user") | |
.password("password") | |
.roles("USER") | |
.build(); | |
// @formatter:on | |
return new InMemoryUserDetailsManager(userDetails); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment