Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de16646

Browse files
committed
update WebSecurityConfig for new Spring Boot version
1 parent 0c82acc commit de16646

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

‎README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ For more detail, please visit:
2121
> [Spring Boot Login and Registration example with MongoDB](https://www.bezkoder.com/spring-boot-mongodb-login-example/)
2222
2323
Working with Front-end:
24-
> [Vue](https://www.bezkoder.com/jwt-vue-vuex-authentication/)
24+
> [Angular 12](https://www.bezkoder.com/angular-12-jwt-auth-httponly-cookie/) / [Angular 13](https://www.bezkoder.com/angular-13-jwt-auth-httponly-cookie/) / [Angular 14](https://www.bezkoder.com/angular-14-jwt-auth/)
2525
26-
> [Angular 8](https://www.bezkoder.com/angular-jwt-authentication/) / [Angular 10](https://www.bezkoder.com/angular-10-jwt-auth/) / [Angular 11](https://www.bezkoder.com/angular-11-jwt-auth/) / [Angular 12](https://www.bezkoder.com/angular-12-jwt-auth/) / [Angular 13](https://www.bezkoder.com/angular-13-jwt-auth/)
27-
28-
> [React](https://www.bezkoder.com/react-jwt-auth/) / [React Redux](https://www.bezkoder.com/react-redux-jwt-auth/)
26+
> [React](https://www.bezkoder.com/react-login-example-jwt-hooks/)
2927
3028
More Practice:
3129
> [Spring Boot with MongoDB CRUD example using Spring Data](https://www.bezkoder.com/spring-boot-mongodb-crud/)
@@ -34,6 +32,23 @@ More Practice:
3432
3533
> [Spring Boot + GraphQL + MongoDB example](https://www.bezkoder.com/spring-boot-graphql-mongodb-example-graphql-java/)
3634
35+
Fullstack:
36+
> [Vue.js + Spring Boot + MongoDB example](https://www.bezkoder.com/spring-boot-vue-mongodb/)
37+
38+
> [Angular 8 + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-spring-boot-mongodb/)
39+
40+
> [Angular 10 + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-10-spring-boot-mongodb/)
41+
42+
> [Angular 11 + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-11-spring-boot-mongodb/)
43+
44+
> [Angular 12 + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-12-spring-boot-mongodb/)
45+
46+
> [Angular 13 + Spring Boot + MongoDB example](https://www.bezkoder.com/angular-13-spring-boot-mongodb/)
47+
48+
> [Angular 14 + Spring Boot + MongoDB example](https://www.bezkoder.com/spring-boot-angular-14-mongodb/)
49+
50+
> [React + Spring Boot + MongoDB example](https://www.bezkoder.com/react-spring-boot-mongodb/)
51+
3752
Run both Back-end & Front-end in one place:
3853
> [Integrate Angular with Spring Boot Rest API](https://www.bezkoder.com/integrate-angular-spring-boot/)
3954

‎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/security/mongodb/security/WebSecurityConfig.java

Lines changed: 52 additions & 18 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.security.mongodb.security.jwt.AuthEntryPointJwt;
1821
import com.bezkoder.spring.security.mongodb.security.jwt.AuthTokenFilter;
1922
import com.bezkoder.spring.security.mongodb.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+
// @Override
43+
// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
44+
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
45+
// }
46+
47+
@Bean
48+
public DaoAuthenticationProvider authenticationProvider() {
49+
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
50+
51+
authProvider.setUserDetailsService(userDetailsService);
52+
authProvider.setPasswordEncoder(passwordEncoder());
53+
54+
return authProvider;
4255
}
4356

57+
// @Bean
58+
// @Override
59+
// public AuthenticationManager authenticationManagerBean() throws Exception {
60+
// return super.authenticationManagerBean();
61+
// }
62+
4463
@Bean
45-
@Override
46-
public AuthenticationManager authenticationManagerBean() throws Exception {
47-
return super.authenticationManagerBean();
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)

AltStyle によって変換されたページ (->オリジナル) /