Spring Security使用授权标签和注解

Spring Security的声明式安全授权有两种方式,一种是以url模式匹配的方式,另一种是方法上使用注解声明权限,这里重点说第二种。

Spring Security默认是禁用注解的,要想开启注解,要在继承WebSecurityConfigurerAdapter的类加@EnableMethodSecurity注解,并在该类中将AuthenticationManager定义为Bean。

@Configuration
@EnableWebSecurity
/**
 *  三种方法级权限控制
 * @author luenxin
 * 1.securedEnabled: Spring Security’s native annotation
 * 2.jsr250Enabled: standards-based and allow simple role-based constraints
 * 3.prePostEnabled: expression-based
 */
@EnableGlobalMethodSecurity(jsr250Enabled=true)
public class CasSecurityConfiguration extends WebSecurityConfigurerAdapter {
 
	@Autowired
	AuthenticationManager authenticationManager;
}

这样就启动了JSR-250的注解支持,我们在方法上使用注解来控制权限。

JSR-250注解

@DenyAll拒绝所有访问。

@RolesAllowed({"USER","ADMIN"})该方法只要具有“USER","ADMIN"任意一种权限就可以访问。这里可以省略前缀ROLE_,实际的权限可能是ROLE_ADMIN

@PermitAll允许所有访问

prePostEnabled注解

这是一种基于表达式的注解,并可以自定义扩展,只需继承GlobalMethodSecurityConfiguration类就可以实现。当然,别忘了在该扩展类上添加注解:@EnableGlobalMethodSecurity(prePostEnabled=true)来启动这种注解支持。如果没有访问方法的权限,会抛出AccessDeniedException。

@PreAuthorize

在方法执行之前执行,而且这里可以调用方法的参数,也可以得到参数值,这里利用JAVA8的参数名反射特性,如果没有JAVA8,那么也可以利用Spring Secuirty的@P标注参数,或利用Spring Data的@Param标注参数

@PreAuthorize("#userId == authentication.principal.userId or hasAuthority(‘ADMIN’)")

void changePassword(@P("userId") long userId ){  }

这里表示在changePassword方法执行之前,判断方法参数userId的值是否等于principal中保存的当前用户的userId,或者当前用户是否具有ROLE_ADMIN权限,两种符合其一,就可以访问该 方法。

@PostAuthorize

在方法执行之后执行,而且这里可以调用方法的返回值,如果EL为false,那么该方法也已经执行完了,可能会回滚。EL变量returnObject表示返回的对象。

@PostAuthorize("returnObject.userId == authentication.principal.userId or hasPermission(returnObject, 'ADMIN')")

User getUser();

@PostFilter

在执行方法之后执行,而且这里可以调用方法的返回值,然后对返回值进行过滤或处理。EL变量returnObject表示返回的对象。只有方法返回的集合或数组类型的才可以使用。(与分页技术不兼容)

@PreFilter

EL变量filterObject表示参数,如果有多个参数,可以使用@filterTarget注解参数,只有方法是集合或数组才行(与分页技术不兼容)。

securedEnabled

@Secured

在方法上指定安全性要求,只有对应角色的用户才可以调用这些方法。

import org.springframework.security.access.annotation.Secured;


public interface UserService {

	List findAllUsers();

	@Secured("ROLE_ADMIN")
	void updateUser(User user);

	@Secured({ "ROLE_DBA", "ROLE_ADMIN" })
	void deleteUser();
	
}

 

最后欢迎大家访问我的个人网站:1024s​​​​​​​

你可能感兴趣的:(秒扒Spring)