|
| 1 | +package com.baeldung.springmvcforms.configuration; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.context.annotation.Bean; |
| 5 | +import org.springframework.context.annotation.Configuration; |
| 6 | +import org.springframework.core.annotation.Order; |
| 7 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 8 | +import org.springframework.security.authentication.TestingAuthenticationProvider; |
| 9 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 10 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 11 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 12 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 13 | +import org.springframework.security.core.userdetails.User; |
| 14 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 15 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 16 | + |
| 17 | +@Configuration |
| 18 | +@EnableWebSecurity |
| 19 | +public class SecurityConfig { |
| 20 | + |
| 21 | + @Bean |
| 22 | + public UserDetailsService userDetailsService() throws Exception { |
| 23 | + InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); |
| 24 | + manager.createUser(User.withUsername("user") |
| 25 | + .password("userPass") |
| 26 | + .roles("USER") |
| 27 | + .build()); |
| 28 | + manager.createUser(User.withUsername("admin") |
| 29 | + .password("adminPass") |
| 30 | + .roles("ADMIN") |
| 31 | + .build()); |
| 32 | + return manager; |
| 33 | + } |
| 34 | + |
| 35 | + @Configuration |
| 36 | + @Order(1) |
| 37 | + public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter { |
| 38 | + |
| 39 | + public App1ConfigurationAdapter() { |
| 40 | + super(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 45 | + auth.inMemoryAuthentication() |
| 46 | + .withUser("admin") |
| 47 | + .password("admin") |
| 48 | + .roles("ADMIN"); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void configure(HttpSecurity http) throws Exception { |
| 53 | + http.antMatcher("/admin*") |
| 54 | + .authorizeRequests() |
| 55 | + .anyRequest() |
| 56 | + .hasRole("ADMIN") |
| 57 | + // log in |
| 58 | + .and() |
| 59 | + .formLogin() |
| 60 | + .loginPage("/loginAdmin") |
| 61 | + .loginProcessingUrl("/admin_login") |
| 62 | + .failureUrl("/loginAdmin?error=loginError") |
| 63 | + .defaultSuccessUrl("/adminPage") |
| 64 | + // logout |
| 65 | + .and() |
| 66 | + .logout() |
| 67 | + .logoutUrl("/admin_logout") |
| 68 | + .logoutSuccessUrl("/protectedLinks") |
| 69 | + .deleteCookies("JSESSIONID") |
| 70 | + .and() |
| 71 | + .exceptionHandling() |
| 72 | + .accessDeniedPage("/403") |
| 73 | + .and() |
| 74 | + .csrf() |
| 75 | + .disable(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Configuration |
| 80 | + @Order(2) |
| 81 | + public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter { |
| 82 | + |
| 83 | + public App2ConfigurationAdapter() { |
| 84 | + super(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 89 | + auth.inMemoryAuthentication() |
| 90 | + .withUser("user") |
| 91 | + .password("user") |
| 92 | + .roles("USER"); |
| 93 | + } |
| 94 | + |
| 95 | + protected void configure(HttpSecurity http) throws Exception { |
| 96 | + http.antMatcher("/user*") |
| 97 | + .authorizeRequests() |
| 98 | + .anyRequest() |
| 99 | + .hasRole("USER") |
| 100 | + // log in |
| 101 | + .and() |
| 102 | + .formLogin() |
| 103 | + .loginPage("/loginUser") |
| 104 | + .loginProcessingUrl("/user_login") |
| 105 | + .failureUrl("/loginUser?error=loginError") |
| 106 | + .defaultSuccessUrl("/userPage") |
| 107 | + // logout |
| 108 | + .and() |
| 109 | + .logout() |
| 110 | + .logoutUrl("/user_logout") |
| 111 | + .logoutSuccessUrl("/protectedLinks") |
| 112 | + .deleteCookies("JSESSIONID") |
| 113 | + .and() |
| 114 | + .exceptionHandling() |
| 115 | + .accessDeniedPage("/403") |
| 116 | + .and() |
| 117 | + .csrf() |
| 118 | + .disable(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments