|
1 | 1 | package com.luv2code.springsecurity.demo.config; |
2 | 2 |
|
| 3 | +import org.springframework.context.annotation.Bean; |
3 | 4 | import org.springframework.context.annotation.Configuration; |
4 | | -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
5 | 5 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
6 | 6 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
7 | | -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
8 | 7 | import org.springframework.security.core.userdetails.User; |
9 | | -import org.springframework.security.core.userdetails.User.UserBuilder; |
| 8 | +import org.springframework.security.core.userdetails.UserDetails; |
| 9 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 10 | +import org.springframework.security.web.SecurityFilterChain; |
10 | 11 |
|
11 | 12 | @Configuration |
12 | 13 | @EnableWebSecurity |
13 | | -public class DemoSecurityConfig extends WebSecurityConfigurerAdapter { |
14 | | - |
15 | | -@Override |
16 | | -protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
17 | | - |
18 | | -// add our users for in memory authentication |
19 | | - |
20 | | -UserBuilder users = User.withDefaultPasswordEncoder(); |
21 | | - |
22 | | -auth.inMemoryAuthentication() |
23 | | -.withUser(users.username("john").password("test123").roles("EMPLOYEE")) |
24 | | -.withUser(users.username("mary").password("test123").roles("EMPLOYEE", "MANAGER")) |
25 | | -.withUser(users.username("susan").password("test123").roles("EMPLOYEE", "ADMIN")); |
26 | | -} |
27 | | - |
28 | | -@Override |
29 | | -protected void configure(HttpSecurity http) throws Exception { |
30 | | - |
31 | | -http.authorizeRequests() |
32 | | -.anyRequest().authenticated() |
33 | | -.and() |
34 | | -.formLogin() |
35 | | -.loginPage("/showMyLoginPage") |
36 | | -.loginProcessingUrl("/authenticateTheUser") |
37 | | -.permitAll() |
38 | | -.and() |
39 | | -.logout().permitAll(); |
40 | | - |
41 | | -} |
42 | | - |
| 14 | +public class DemoSecurityConfig { |
| 15 | + |
| 16 | + @Bean |
| 17 | + public InMemoryUserDetailsManager userDetailsService() { |
| 18 | + |
| 19 | + UserDetails john = User.builder() |
| 20 | + .username("john") |
| 21 | + .password("{noop}test123") |
| 22 | + .roles("EMPLOYEE") |
| 23 | + .build(); |
| 24 | + |
| 25 | + UserDetails mary = User.builder() |
| 26 | + .username("mary") |
| 27 | + .password("{noop}test123") |
| 28 | + .roles("EMPLOYEE", "MANAGER") |
| 29 | + .build(); |
| 30 | + |
| 31 | + UserDetails susan = User.builder() |
| 32 | + .username("susan") |
| 33 | + .password("{noop}test123") |
| 34 | + .roles("EMPLOYEE", "ADMIN") |
| 35 | + .build(); |
| 36 | + |
| 37 | + return new InMemoryUserDetailsManager(john, mary, susan); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 43 | + |
| 44 | + return http |
| 45 | + .authorizeRequests(configurer -> |
| 46 | + configurer |
| 47 | + .antMatchers("/").hasRole("EMPLOYEE") |
| 48 | + .antMatchers("/leaders/**").hasRole("MANAGER") |
| 49 | + .antMatchers("/systems/**").hasRole("ADMIN")) |
| 50 | + |
| 51 | + .formLogin(configurer -> |
| 52 | + configurer |
| 53 | + .loginPage("/showMyLoginPage") |
| 54 | + .loginProcessingUrl("/authenticateTheUser") |
| 55 | + .permitAll()) |
| 56 | + |
| 57 | + .logout(configurer -> |
| 58 | + configurer |
| 59 | + .permitAll()) |
| 60 | + |
| 61 | + .build(); |
| 62 | + |
| 63 | + } |
| 64 | + |
43 | 65 | } |
44 | 66 |
|
45 | 67 |
|
46 | 68 |
|
47 | 69 |
|
48 | 70 |
|
49 | 71 |
|
| 72 | + |
| 73 | + |
| 74 | + |
0 commit comments