Skip to content

Commit 822423f

Browse files
committed
Added: config with hasAnyRoles
1 parent d4d6301 commit 822423f

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bezkoder.springjwt.controllers;
2+
3+
import org.springframework.web.bind.annotation.CrossOrigin;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@CrossOrigin(origins = "*", maxAge = 3600)
9+
@RestController
10+
@RequestMapping("/api/test2")
11+
public class TestAlternativeController {
12+
13+
@GetMapping("/user")
14+
public String userAccess() {
15+
return "User 2 Content.";
16+
}
17+
18+
@GetMapping("/mod")
19+
public String moderatorAccess() {
20+
return "Moderator 2 Board.";
21+
}
22+
23+
@GetMapping("/admin")
24+
public String adminAccess() {
25+
return "Admin 2 Board.";
26+
}
27+
28+
}

src/main/java/com/bezkoder/springjwt/controllers/TestController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public String moderatorAccess() {
3232
public String adminAccess() {
3333
return "Admin Board.";
3434
}
35+
36+
@GetMapping("/admin2")
37+
// @PreAuthorize("hasRole('ADMIN')")
38+
public String adminAccess2() {
39+
return "Admin 2 Board.";
40+
}
41+
3542
}
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
package com.bezkoder.springjwt.security;
22

3+
import com.bezkoder.springjwt.security.jwt.AuthEntryPointJwt;
4+
import com.bezkoder.springjwt.security.jwt.AuthTokenFilter;
5+
import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl;
36
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.context.annotation.Bean;
58
import org.springframework.context.annotation.Configuration;
69
import org.springframework.security.authentication.AuthenticationManager;
710
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
8-
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
911
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
1012
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1113
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
12-
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13-
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1414
import org.springframework.security.config.http.SessionCreationPolicy;
1515
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1616
import org.springframework.security.crypto.password.PasswordEncoder;
1717
import org.springframework.security.web.SecurityFilterChain;
1818
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1919

20-
import com.bezkoder.springjwt.security.jwt.AuthEntryPointJwt;
21-
import com.bezkoder.springjwt.security.jwt.AuthTokenFilter;
22-
import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl;
23-
20+
// ERG BELANGRIJK -- sinds Spring Boot 2.7 is de inrichting van Security anders geworden.
21+
// Wat is gecommentarieerd ... was < 2.7.
2422
@Configuration
2523
@EnableGlobalMethodSecurity(
26-
// securedEnabled = true,
27-
// jsr250Enabled = true,
28-
prePostEnabled = true)
24+
// securedEnabled = true, was < SB 2.7
25+
// jsr250Enabled = true, was < SB 2.7
26+
prePostEnabled = true)
2927
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
3028
@Autowired
3129
UserDetailsServiceImpl userDetailsService;
@@ -42,23 +40,21 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
4240
// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
4341
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
4442
// }
45-
43+
4644
@Bean
4745
public DaoAuthenticationProvider authenticationProvider() {
48-
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
49-
50-
authProvider.setUserDetailsService(userDetailsService);
51-
authProvider.setPasswordEncoder(passwordEncoder());
52-
53-
return authProvider;
46+
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
47+
authProvider.setUserDetailsService(userDetailsService);
48+
authProvider.setPasswordEncoder(passwordEncoder());
49+
return authProvider;
5450
}
5551

5652
// @Bean
5753
// @Override
5854
// public AuthenticationManager authenticationManagerBean() throws Exception {
5955
// return super.authenticationManagerBean();
6056
// }
61-
57+
6258
@Bean
6359
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
6460
return authConfig.getAuthenticationManager();
@@ -80,20 +76,23 @@ public PasswordEncoder passwordEncoder() {
8076
//
8177
// http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
8278
// }
83-
79+
8480
@Bean
8581
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
8682
http.cors().and().csrf().disable()
87-
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
88-
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
89-
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
90-
.antMatchers("/api/test/**").permitAll()
91-
.anyRequest().authenticated();
92-
83+
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
84+
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
85+
.authorizeRequests().antMatchers("/api/auth/**").permitAll()
86+
.antMatchers("/api/test/**").permitAll()
87+
.antMatchers("/api/test2/user").hasAnyRole("USER", "MODERATOR", "ADMIN")
88+
.antMatchers("/api/test2/mod").hasAnyRole("MODERATOR")
89+
.antMatchers("/api/test2/admin").hasRole("ADMIN")
90+
.anyRequest().authenticated();
91+
9392
http.authenticationProvider(authenticationProvider());
9493

9594
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
96-
95+
9796
return http.build();
9897
}
9998
}

0 commit comments

Comments
 (0)