Skip to content

Commit 738a8fd

Browse files
committed
Updated to Spring Security 5.7.3 - resolved deprecated code
1 parent f1da36f commit 738a8fd

File tree

2 files changed

+52
-38
lines changed
  • 07-spring-security-5/solution-code-spring-security-demo-07-user-roles-restrict-access

2 files changed

+52
-38
lines changed

07-spring-security-5/solution-code-spring-security-demo-07-user-roles-restrict-access/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.luv2code</groupId>
6-
<artifactId>spring-security-demo</artifactId>
6+
<artifactId>spring-security-demo-07-user-roles-restrict-access</artifactId>
77
<version>1.0</version>
88
<packaging>war</packaging>
99

1010
<name>spring-security-demo</name>
1111

1212
<properties>
13-
<springframework.version>5.0.2.RELEASE</springframework.version>
14-
<springsecurity.version>5.0.0.RELEASE</springsecurity.version>
13+
<springframework.version>5.3.22</springframework.version>
14+
<springsecurity.version>5.7.3</springsecurity.version>
1515

1616
<maven.compiler.source>1.8</maven.compiler.source>
1717
<maven.compiler.target>1.8</maven.compiler.target>
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,65 @@
11
package com.luv2code.springsecurity.demo.config;
22

3+
import org.springframework.context.annotation.Bean;
34
import org.springframework.context.annotation.Configuration;
4-
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
55
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
66
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
87
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;
1011

1112
@Configuration
1213
@EnableWebSecurity
13-
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
14+
public class DemoSecurityConfig {
1415

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-
.antMatchers("/").hasRole("EMPLOYEE")
33-
.antMatchers("/leaders/**").hasRole("MANAGER")
34-
.antMatchers("/systems/**").hasRole("ADMIN")
35-
.and()
36-
.formLogin()
37-
.loginPage("/showMyLoginPage")
38-
.loginProcessingUrl("/authenticateTheUser")
39-
.permitAll()
40-
.and()
41-
.logout().permitAll();
42-
43-
}
44-
45-
}
16+
@Bean
17+
public InMemoryUserDetailsManager userDetailsService() {
18+
19+
UserDetails john = User.builder()
20+
.username("john")
21+
.password("{noop}test123")
22+
.roles("EMPLOYEE")
23+
.build();
4624

25+
UserDetails mary = User.builder()
26+
.username("mary")
27+
.password("{noop}test123")
28+
.roles("EMPLOYEE", "MANAGER")
29+
.build();
4730

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+
}
4840

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"))
4950

51+
.formLogin(configurer ->
52+
configurer
53+
.loginPage("/showMyLoginPage")
54+
.loginProcessingUrl("/authenticateTheUser")
55+
.permitAll())
5056

57+
.logout(configurer ->
58+
configurer
59+
.permitAll())
5160

61+
.build();
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)