|
@@ -4,11 +4,18 @@ import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+import org.apereo.cas.client.validation.Cas30ServiceTicketValidator;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
+import org.springframework.security.authentication.ProviderManager;
|
|
|
|
|
+import org.springframework.security.cas.ServiceProperties;
|
|
|
|
|
+import org.springframework.security.cas.authentication.CasAuthenticationProvider;
|
|
|
|
|
+import org.springframework.security.cas.web.CasAuthenticationEntryPoint;
|
|
|
|
|
+import org.springframework.security.cas.web.CasAuthenticationFilter;
|
|
|
|
|
+import es.uv.saic.service.CasUserDetailService;
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
@@ -21,6 +28,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
|
|
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager;
|
|
import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager;
|
|
|
|
|
+import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
|
|
import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
|
|
import org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy;
|
|
|
import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
|
|
import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
|
|
|
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
|
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
|
@@ -42,25 +50,33 @@ public class SecurityConfig {
|
|
|
private String validIp;
|
|
private String validIp;
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
|
|
|
|
+ public SecurityFilterChain filterChain(HttpSecurity http, CasAuthenticationFilter casFilter, CasAuthenticationEntryPoint casEntryPoint) throws Exception {
|
|
|
http.authorizeHttpRequests((auth) -> auth
|
|
http.authorizeHttpRequests((auth) -> auth
|
|
|
.requestMatchers("/", "/css/**", "/js/**", "/img/**", "/logos/*", "/logos/**").permitAll()
|
|
.requestMatchers("/", "/css/**", "/js/**", "/img/**", "/logos/*", "/logos/**").permitAll()
|
|
|
- .requestMatchers("/login**").permitAll()
|
|
|
|
|
|
|
+ .requestMatchers("/login").permitAll()
|
|
|
|
|
+ .requestMatchers("/login/cas").authenticated()
|
|
|
.requestMatchers("/keepalive").permitAll()
|
|
.requestMatchers("/keepalive").permitAll()
|
|
|
.requestMatchers("/public/**").permitAll()
|
|
.requestMatchers("/public/**").permitAll()
|
|
|
.requestMatchers("/actuator/**").access(new WebExpressionAuthorizationManager("hasIpAddress('" + this.validIp + "')"))
|
|
.requestMatchers("/actuator/**").access(new WebExpressionAuthorizationManager("hasIpAddress('" + this.validIp + "')"))
|
|
|
.requestMatchers("/actuator/**").access(new WebExpressionAuthorizationManager("hasIpAddress('127.0.0.1')"))
|
|
.requestMatchers("/actuator/**").access(new WebExpressionAuthorizationManager("hasIpAddress('127.0.0.1')"))
|
|
|
|
|
+ .anyRequest().authenticated()
|
|
|
)
|
|
)
|
|
|
- .authorizeHttpRequests((auth)-> auth
|
|
|
|
|
- .anyRequest().authenticated()
|
|
|
|
|
- //.anyRequest().permitAll()
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ .addFilter(casFilter)
|
|
|
|
|
+ .exceptionHandling(ex -> ex
|
|
|
|
|
+ .defaultAuthenticationEntryPointFor(
|
|
|
|
|
+ casEntryPoint,
|
|
|
|
|
+ request -> request.getServletPath().startsWith("/login/cas")
|
|
|
|
|
+ )
|
|
|
|
|
+ .defaultAuthenticationEntryPointFor(
|
|
|
|
|
+ new LoginUrlAuthenticationEntryPoint("/login"),
|
|
|
|
|
+ request -> !request.getServletPath().startsWith("/login/cas")
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
.formLogin((form) -> form
|
|
.formLogin((form) -> form
|
|
|
.loginPage("/login")
|
|
.loginPage("/login")
|
|
|
.defaultSuccessUrl("/procedures?_new=1",true)
|
|
.defaultSuccessUrl("/procedures?_new=1",true)
|
|
|
.failureUrl("/login?error=noauth")
|
|
.failureUrl("/login?error=noauth")
|
|
|
.successHandler(new AuthSuccessHandler())
|
|
.successHandler(new AuthSuccessHandler())
|
|
|
- .permitAll()
|
|
|
|
|
)
|
|
)
|
|
|
.logout((logout) -> logout
|
|
.logout((logout) -> logout
|
|
|
.logoutSuccessUrl("/login")
|
|
.logoutSuccessUrl("/login")
|
|
@@ -87,24 +103,16 @@ public class SecurityConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
- public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
|
|
|
|
|
- return http.getSharedObject(AuthenticationManagerBuilder.class)
|
|
|
|
|
- .authenticationProvider(authProvider)
|
|
|
|
|
- .build();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ public AuthenticationManager authenticationManager(AuthProvider authProvider, CasAuthenticationProvider casAuthProvider) {
|
|
|
|
|
+ return new ProviderManager(authProvider, casAuthProvider);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Bean
|
|
@Bean
|
|
|
public SessionRegistry sessionRegistry() {
|
|
public SessionRegistry sessionRegistry() {
|
|
|
return new SessionRegistryImpl();
|
|
return new SessionRegistryImpl();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
- public DefaultSpringSecurityContextSource contextSource() {
|
|
|
|
|
- return new DefaultSpringSecurityContextSource(
|
|
|
|
|
- Collections.singletonList("ldap://ldap.uv.es"), "dc=uv,dc=es");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Bean
|
|
|
|
|
public PasswordEncoder passwordEncoder() {
|
|
public PasswordEncoder passwordEncoder() {
|
|
|
return new BCryptPasswordEncoder();
|
|
return new BCryptPasswordEncoder();
|
|
|
}
|
|
}
|
|
@@ -114,6 +122,56 @@ public class SecurityConfig {
|
|
|
return new HttpSessionEventPublisher();
|
|
return new HttpSessionEventPublisher();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public ServiceProperties serviceProperties() {
|
|
|
|
|
+ ServiceProperties sp = new ServiceProperties();
|
|
|
|
|
+ sp.setService("http://localhost:8080/login/cas");
|
|
|
|
|
+ return sp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public CasAuthenticationEntryPoint casAuthenticationEntryPoint(ServiceProperties sp) {
|
|
|
|
|
+ CasAuthenticationEntryPoint cp = new CasAuthenticationEntryPoint();
|
|
|
|
|
+ cp.setLoginUrl("http://localhost:8085/cas/login");
|
|
|
|
|
+ cp.setServiceProperties(sp);
|
|
|
|
|
+ return cp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public CasAuthenticationFilter casAuthenticationFilter(
|
|
|
|
|
+ AuthenticationManager authManager,
|
|
|
|
|
+ ServiceProperties sp,
|
|
|
|
|
+ CasAuthenticationEntryPoint casEntryPoint) {
|
|
|
|
|
+
|
|
|
|
|
+ CasAuthenticationFilter filter = new CasAuthenticationFilter();
|
|
|
|
|
+ filter.setAuthenticationManager(authManager);
|
|
|
|
|
+ filter.setServiceProperties(sp);
|
|
|
|
|
+ filter.setFilterProcessesUrl("/login/cas");
|
|
|
|
|
+
|
|
|
|
|
+ filter.setAuthenticationFailureHandler((request, response, exception) -> {
|
|
|
|
|
+ casEntryPoint.commence(request, response, exception);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return filter;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public CasAuthenticationProvider casAuthenticationProvider(
|
|
|
|
|
+ ServiceProperties sp,
|
|
|
|
|
+ CasUserDetailService userDetailsService) {
|
|
|
|
|
+
|
|
|
|
|
+ CasAuthenticationProvider provider = new CasAuthenticationProvider();
|
|
|
|
|
+ provider.setAuthenticationUserDetailsService(userDetailsService);
|
|
|
|
|
+ provider.setServiceProperties(sp);
|
|
|
|
|
+
|
|
|
|
|
+ provider.setTicketValidator(new Cas30ServiceTicketValidator("http://localhost:8085/cas"));
|
|
|
|
|
+
|
|
|
|
|
+ provider.setKey("CAS_PROVIDER_SAIC_LOCAL");
|
|
|
|
|
+
|
|
|
|
|
+ return provider;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Bean
|
|
@Bean
|
|
|
public CompositeSessionAuthenticationStrategy concurrentSession() {
|
|
public CompositeSessionAuthenticationStrategy concurrentSession() {
|
|
|
|
|
|