Browse Source

Persistent sesions

dagarcos 2 years ago
parent
commit
782e0b790e
1 changed files with 18 additions and 1 deletions
  1. 18 1
      src/main/java/es/uv/garcosda/config/WebSecurityConfig.java

+ 18 - 1
src/main/java/es/uv/garcosda/config/WebSecurityConfig.java

@@ -4,6 +4,9 @@
 package es.uv.garcosda.config;
 
 
+import javax.sql.DataSource;
+
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -11,12 +14,17 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.security.web.SecurityFilterChain;
+import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
+import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
 import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 
 @Configuration
 @EnableWebSecurity
 public class WebSecurityConfig {
 	
+	@Autowired
+	private DataSource dataSource;
+	
 	@Bean
     public PasswordEncoder passwordEncoder() {
         return new BCryptPasswordEncoder();
@@ -42,9 +50,18 @@ public class WebSecurityConfig {
 	        	.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
 	        	.logoutSuccessUrl("/login?logout")
 	        	.deleteCookies("my-remember-me-cookie")
-	            .permitAll();
+	            .permitAll()
+            .and()
+	        .exceptionHandling()
+	        	.accessDeniedPage("/403");
 		
 		return http.build();
 	}
+	
+	PersistentTokenRepository persistentTokenRepository(){
+    	JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl();
+    	tokenRepositoryImpl.setDataSource(dataSource);
+    	return tokenRepositoryImpl;
+    }
 		
 }