Spring security 3中关于方法级的权限控制有两个方法
1)使用注解 @PreAuthorize 和 @PostAuthorize,要先在配置文件中启用:
<global-method-security secured-annotations="enabled" />
然后使用方法为:
@Repository
public class EmployeeDaoImpl implements EmployeeDAO {
@Autowired
private SessionFactory sessionFactory;
@PreAuthorize("hasRole('ROLE_ADMIN')")
@Override
public void addEmployee(EmployeeEntity employee) {
//System.out.println(((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAuthorities());
this.sessionFactory.getCurrentSession().save(employee);
}
2) 使用XML灵活配置,配置i文件中如下:
<global-method-security>标签:PreAuthorize,Spring,sessionFactory,employee,security,权限,方法 From: https://blog.51cto.com/u_14230175/5905576
<protect-pointcut expression="execution(* com.howtodoinjava.service.*Impl.add*(..))" access="ROLE_USER"/>
<protect-pointcut expression="execution(* com.howtodoinjava.service.*Impl.delete*(..))" access="ROLE_ADMIN"/>
</global-method-security>