Going through Spring Security I created a method:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/static/build/app.js", "/static/app/styles/*/**", "/static/app/js/*/**",
"/static/build/libs.js", "/index.html", "/static/build/*/**", "/", "/static/**").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/api/user/registerClient").permitAll()
.antMatchers("/api/user/checklogin/**").permitAll()
.antMatchers("/api/user/getAllAdmins").permitAll()
.antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/")
.permitAll();
And example of the controller method:
@RequestMapping(value = "/api/vehicle")
@PreAuthorize("hasAnyAuthority('ADMIN', 'CLIENT')")
@RequestMapping(value = "", method = RequestMethod.GET)
public List<VehicleReservationModel> getVehiclesForClientByLogin(HttpServletRequest request) {
Principal name = request.getUserPrincipal();
if (name.getName() == null) {
throw new RuntimeException("Brak sesji");
}
if (roleService.getRoleForUserByLogin(name.getName()).toLowerCase().equals("admin")) {
return vehicleService.getAllVehicles();
} else {
List<VehicleReservationModel> vehicleList = vehicleService.getVehiclesForClientByLogin(name.getName());
if (vehicleList == null) {
throw new RuntimeException("Brak pojazdów dla klienta " + name.getName() + " - lista jest pusta");
}
return vehicleList;
}
}
The case is whenever I remove the ADMIN from
@PreAuthorize("hasAnyAuthority('ADMIN', 'CLIENT')")
and comment:
.antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
it always let me in into the API. I thought whenever I create some privilidges, it will always work. Why in above example my Spring Security doesnt work?
UPDATE:
The answear is to enable using annotation PreAuthorize you need to add :
@EnableGlobalMethodSecurity(prePostEnabled = true)
You enabled @Secured with EnableGlobalMethodSecurity#securedEnabled:
Determines if Spring Security's Secured annotations should be enabled.
but you have to enable @PreAuthorize with EnableGlobalMethodSecurity#prePostEnabled:
Determines if Spring Security's pre post annotations should be enabled. Default is false.
Your modified Spring Security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/static/build/app.js", "/static/app/styles/*/**", "/static/app/js/*/**",
"/static/build/libs.js", "/index.html", "/static/build/*/**", "/", "/static/**").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/api/user/registerClient").permitAll()
.antMatchers("/api/user/checklogin/**").permitAll()
.antMatchers("/api/user/getAllAdmins").permitAll()
// .antMatchers("/api/**").hasAnyAuthority(AuthoritiesConstants.CLIENT, AuthoritiesConstants.ADMIN, AuthoritiesConstants.WORKER)
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/")
.permitAll();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With