在controller层中,不同的方法根据不同的权限进行拦截,在sevice层中如果也想根据角色权限调用不同的方法应该怎么做呢?
首先在security配置类上加注解
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
prePostEnabled注解:相当于开启了两个注解,一个方法前进行校验,一个方法后进行校验
securedEnabled注解
在service层方法上加上注解,只有拥有相关权限才能调用方法
@Service
public class HelloService {
@PreAuthorize("hasRole('admin')")
public String hello1() {
return "admin,hello service";
}
@PreAuthorize("hasAnyRole('admin','user')")
public String hello2() {
return "任意 admin,user,hello service";
}
@Secured("ROLE_user")
public String hello3() {
return "hello user";
}
}