Skip to content

Commit f5a9253

Browse files
committed
update WebSecurityConfig for new Spring Boot version
1 parent 65d74a8 commit f5a9253

File tree

3 files changed

+81
-32
lines changed

3 files changed

+81
-32
lines changed

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ INSERT INTO roles(name) VALUES('ROLE_ADMIN');
8686

8787
[Spring Boot Refresh Token with JWT example](https://www.bezkoder.com/spring-boot-refresh-token-jwt/)
8888

89+
## More Practice:
90+
> [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/)
91+
92+
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)
93+
94+
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
95+
96+
> [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/)
97+
98+
Associations:
99+
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
100+
101+
> [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/)
102+
103+
> [JPA One To One example with Spring Boot](https://www.bezkoder.com/jpa-one-to-one/)
104+
105+
Deployment:
106+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
107+
108+
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)
109+
89110
## Fullstack CRUD App
90111

91112
> [Vue.js + Spring Boot + H2 Embedded database example](https://www.bezkoder.com/spring-boot-vue-js-crud-example/)
@@ -120,6 +141,12 @@ INSERT INTO roles(name) VALUES('ROLE_ADMIN');
120141
121142
> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/)
122143
144+
> [Angular 14 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-14-crud/)
145+
146+
> [Angular 14 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-14-mysql/)
147+
148+
> [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/)
149+
123150
> [React + Spring Boot + MySQL example](https://www.bezkoder.com/react-spring-boot-crud/)
124151
125152
> [React + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-react-postgresql/)
@@ -132,15 +159,3 @@ Run both Back-end & Front-end in one place:
132159
> [Integrate React.js with Spring Boot Rest API](https://www.bezkoder.com/integrate-reactjs-spring-boot/)
133160
134161
> [Integrate Vue.js with Spring Boot Rest API](https://www.bezkoder.com/integrate-vue-spring-boot/)
135-
136-
More Practice:
137-
> [Spring Boot File upload example with Multipart File](https://www.bezkoder.com/spring-boot-file-upload/)
138-
139-
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://www.bezkoder.com/spring-boot-restcontrolleradvice/)
140-
141-
> [Spring Boot Repository Unit Test with @DataJpaTest](https://www.bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
142-
143-
Deployment:
144-
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
145-
146-
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.6.1</version>
8+
<version>2.7.3</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.bezkoder</groupId>

src/main/java/com/bezkoder/spring/login/security/WebSecurityConfig.java

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,30 @@
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
66
import org.springframework.security.authentication.AuthenticationManager;
7-
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
7+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
8+
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
810
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
911
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10-
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13+
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1214
import org.springframework.security.config.http.SessionCreationPolicy;
1315
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1416
import org.springframework.security.crypto.password.PasswordEncoder;
17+
import org.springframework.security.web.SecurityFilterChain;
1518
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1619

1720
import com.bezkoder.spring.login.security.jwt.AuthEntryPointJwt;
1821
import com.bezkoder.spring.login.security.jwt.AuthTokenFilter;
1922
import com.bezkoder.spring.login.security.services.UserDetailsServiceImpl;
2023

2124
@Configuration
22-
@EnableWebSecurity
25+
//@EnableWebSecurity
2326
@EnableGlobalMethodSecurity(
2427
// securedEnabled = true,
2528
// jsr250Enabled = true,
2629
prePostEnabled = true)
27-
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
30+
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
2831
@Autowired
2932
UserDetailsServiceImpl userDetailsService;
3033

@@ -36,31 +39,62 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
3639
return new AuthTokenFilter();
3740
}
3841

39-
@Override
40-
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
41-
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
42-
}
42+
// @Override
43+
// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
44+
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
45+
// }
4346

4447
@Bean
45-
@Override
46-
public AuthenticationManager authenticationManagerBean() throws Exception {
47-
return super.authenticationManagerBean();
48+
public DaoAuthenticationProvider authenticationProvider() {
49+
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
50+
51+
authProvider.setUserDetailsService(userDetailsService);
52+
authProvider.setPasswordEncoder(passwordEncoder());
53+
54+
return authProvider;
55+
}
56+
57+
// @Bean
58+
// @Override
59+
// public AuthenticationManager authenticationManagerBean() throws Exception {
60+
// return super.authenticationManagerBean();
61+
// }
62+
63+
@Bean
64+
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
65+
return authConfig.getAuthenticationManager();
4866
}
4967

5068
@Bean
5169
public PasswordEncoder passwordEncoder() {
5270
return new BCryptPasswordEncoder();
5371
}
5472

55-
@Override
56-
protected void configure(HttpSecurity http) throws Exception {
73+
// @Override
74+
// protected void configure(HttpSecurity http) throws Exception {
75+
// http.cors().and().csrf().disable()
76+
// .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
77+
// .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
78+
// .authorizeRequests().antMatchers("/api/auth/**").permitAll()
79+
// .antMatchers("/api/test/**").permitAll()
80+
// .anyRequest().authenticated();
81+
//
82+
// http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
83+
// }
84+
85+
@Bean
86+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
5787
http.cors().and().csrf().disable()
58-
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
59-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
60-
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
61-
.antMatchers("/api/test/**").permitAll()
62-
.anyRequest().authenticated();
88+
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
89+
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
90+
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
91+
.antMatchers("/api/test/**").permitAll()
92+
.anyRequest().authenticated();
93+
94+
http.authenticationProvider(authenticationProvider());
6395

6496
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
97+
98+
return http.build();
6599
}
66100
}

0 commit comments

Comments
 (0)