|
29 | 29 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; |
30 | 30 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
31 | 31 |
|
32 | | -import java.util.Collection; |
33 | 32 | import java.util.HashSet; |
34 | 33 | import java.util.Objects; |
35 | 34 | import java.util.Set; |
|
41 | 40 | @ConditionalOnExpression("{'saml','saml_plus_basic'}.contains('${authenticate}')") |
42 | 41 | public class Saml2SecurityConfig { |
43 | 42 |
|
44 | | - private static final String LOGOUT_URL = "/logout"; |
45 | | - |
46 | | - private final SecurityRepository securityRepository; |
47 | | - |
48 | | - @Value("${saml.idp.metadata.attribute.role:Role}") |
49 | | - private String roleAttributeName; |
50 | | - |
51 | | - @Value("${saml.logout.url}") |
52 | | - private String successfullLogoutUrl; |
53 | | - |
54 | | - @Autowired |
55 | | - public Saml2SecurityConfig(SecurityRepository securityRepository) { |
56 | | - this.securityRepository = securityRepository; |
57 | | - } |
58 | | - |
59 | | - @Bean |
60 | | - @ConditionalOnProperty(value = "authenticate", havingValue = "saml") |
61 | | - public SecurityFilterChain samlFilterChain(HttpSecurity http, |
62 | | - RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) throws Exception { |
63 | | - return http.csrf(AbstractHttpConfigurer::disable).cors(Customizer.withDefaults()) |
64 | | - .authorizeHttpRequests(auth -> auth.requestMatchers("/api/health", "/images/**", "/js/**", "/login") |
65 | | - .permitAll().anyRequest().authenticated()) |
66 | | - .exceptionHandling(eh -> eh.defaultAuthenticationEntryPointFor( |
67 | | - new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), AntPathRequestMatcher.antMatcher("/api/**"))) |
68 | | - .saml2Login(withDefaults()) |
69 | | - .logout(logout -> logout.logoutUrl(LOGOUT_URL).logoutSuccessUrl(successfullLogoutUrl)).build(); |
70 | | - } |
71 | | - |
72 | | - @Bean |
73 | | - public OpenSaml4AuthenticationProvider openSaml4AuthenticationProvider() { |
74 | | - OpenSaml4AuthenticationProvider authenticationProvider = new OpenSaml4AuthenticationProvider(); |
75 | | - authenticationProvider.setResponseAuthenticationConverter(rolesConverter()); |
76 | | - return authenticationProvider; |
77 | | - } |
78 | | - |
79 | | - private Converter<OpenSaml4AuthenticationProvider.ResponseToken, Saml2Authentication> rolesConverter() { |
80 | | - |
81 | | - Converter<OpenSaml4AuthenticationProvider.ResponseToken, Saml2Authentication> delegate = OpenSaml4AuthenticationProvider |
82 | | - .createDefaultResponseAuthenticationConverter(); |
83 | | - |
84 | | - return (responseToken) -> { |
85 | | - Saml2Authentication authentication = delegate.convert(responseToken); |
86 | | - var principal = (Saml2AuthenticatedPrincipal) Objects.requireNonNull(authentication).getPrincipal(); |
87 | | - |
88 | | - String username = (String) principal.getAttribute("username").getFirst(); |
89 | | - |
90 | | - User cbioUser = securityRepository.getPortalUser(username); |
91 | | - |
92 | | - if (cbioUser == null) { |
93 | | - Saml2Authentication sm2fail = new Saml2Authentication(principal, authentication.getSaml2Response(), |
94 | | - new HashSet<>()); |
95 | | - sm2fail.setAuthenticated(false); |
96 | | - return sm2fail; |
97 | | - } |
98 | | - |
99 | | - UserAuthorities authorities = securityRepository.getPortalUserAuthorities(username); |
100 | | - |
101 | | - Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); |
102 | | - if (!Objects.isNull(authorities)) { |
103 | | - mappedAuthorities.addAll(AuthorityUtils.createAuthorityList(authorities.getAuthorities())); |
104 | | - } |
105 | | - return new Saml2Authentication(principal, authentication.getSaml2Response(), mappedAuthorities); |
106 | | - }; |
107 | | - } |
108 | | - |
109 | | - @Bean |
110 | | - public LogoutSuccessHandler logoutSuccessHandler( |
111 | | - RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) { |
112 | | - // Perform logout at the SAML2 IDP |
113 | | - DefaultRelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver( |
114 | | - relyingPartyRegistrationRepository); |
115 | | - OpenSaml4LogoutRequestResolver logoutRequestResolver = new OpenSaml4LogoutRequestResolver( |
116 | | - relyingPartyRegistrationResolver); |
117 | | - |
118 | | - return new Saml2RelyingPartyInitiatedLogoutSuccessHandler(logoutRequestResolver); |
119 | | - } |
| 43 | + private static final String LOGOUT_URL = "/logout"; |
| 44 | + |
| 45 | + private final SecurityRepository securityRepository; |
| 46 | + |
| 47 | + @Value("${saml.idp.metadata.attribute.role:Role}") |
| 48 | + private String roleAttributeName; |
| 49 | + |
| 50 | + @Value("${saml.logout.url}") |
| 51 | + private String successfullLogoutUrl; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + public Saml2SecurityConfig(SecurityRepository securityRepository) { |
| 55 | + this.securityRepository = securityRepository; |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + @ConditionalOnProperty(value = "authenticate", havingValue = "saml") |
| 60 | + public SecurityFilterChain samlFilterChain(HttpSecurity http, |
| 61 | + RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) throws Exception { |
| 62 | + return http.csrf(AbstractHttpConfigurer::disable).cors(Customizer.withDefaults()) |
| 63 | + .authorizeHttpRequests(auth -> auth.requestMatchers("/api/health", "/images/**", "/js/**", "/login") |
| 64 | + .permitAll().anyRequest().authenticated()) |
| 65 | + .exceptionHandling(eh -> eh.defaultAuthenticationEntryPointFor( |
| 66 | + new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), AntPathRequestMatcher.antMatcher("/api/**"))) |
| 67 | + .saml2Login(withDefaults()) |
| 68 | + .logout(logout -> logout.logoutUrl(LOGOUT_URL).logoutSuccessUrl(successfullLogoutUrl)).build(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public OpenSaml4AuthenticationProvider openSaml4AuthenticationProvider() { |
| 73 | + OpenSaml4AuthenticationProvider authenticationProvider = new OpenSaml4AuthenticationProvider(); |
| 74 | + authenticationProvider.setResponseAuthenticationConverter(rolesConverter()); |
| 75 | + return authenticationProvider; |
| 76 | + } |
| 77 | + |
| 78 | + private Converter<OpenSaml4AuthenticationProvider.ResponseToken, Saml2Authentication> rolesConverter() { |
| 79 | + |
| 80 | + Converter<OpenSaml4AuthenticationProvider.ResponseToken, Saml2Authentication> delegate = OpenSaml4AuthenticationProvider |
| 81 | + .createDefaultResponseAuthenticationConverter(); |
| 82 | + |
| 83 | + return (responseToken) -> { |
| 84 | + Saml2Authentication authentication = delegate.convert(responseToken); |
| 85 | + var principal = (Saml2AuthenticatedPrincipal) Objects.requireNonNull(authentication).getPrincipal(); |
| 86 | + |
| 87 | + String username = (String) principal.getAttribute("username").getFirst(); |
| 88 | + |
| 89 | + User cbioUser = securityRepository.getPortalUser(username); |
| 90 | + |
| 91 | + if (cbioUser == null) { |
| 92 | + Saml2Authentication sm2fail = new Saml2Authentication(principal, authentication.getSaml2Response(), |
| 93 | + new HashSet<>()); |
| 94 | + sm2fail.setAuthenticated(false); |
| 95 | + return sm2fail; |
| 96 | + } |
| 97 | + |
| 98 | + UserAuthorities authorities = securityRepository.getPortalUserAuthorities(username); |
| 99 | + |
| 100 | + Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); |
| 101 | + if (!Objects.isNull(authorities)) { |
| 102 | + mappedAuthorities.addAll(AuthorityUtils.createAuthorityList(authorities.getAuthorities())); |
| 103 | + } |
| 104 | + return new Saml2Authentication(principal, authentication.getSaml2Response(), mappedAuthorities); |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public LogoutSuccessHandler logoutSuccessHandler( |
| 110 | + RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) { |
| 111 | + // Perform logout at the SAML2 IDP |
| 112 | + DefaultRelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver( |
| 113 | + relyingPartyRegistrationRepository); |
| 114 | + OpenSaml4LogoutRequestResolver logoutRequestResolver = new OpenSaml4LogoutRequestResolver( |
| 115 | + relyingPartyRegistrationResolver); |
| 116 | + |
| 117 | + return new Saml2RelyingPartyInitiatedLogoutSuccessHandler(logoutRequestResolver); |
| 118 | + } |
120 | 119 | } |
0 commit comments