spring security中@PreAuthorize注解的使用

添加依赖
<!-- oauth -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
</dependency>

配置oauth
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class OAuth2ResourceServerConfig extends GlobalMethodSecurityConfiguration
{
@Override
protected MethodSecurityExpressionHandler createExpressionHandler()
{
OAuth2MethodSecurityExpressionHandler oAuth2MethodSecurityExpressionHandler = new OAuth2MethodSecurityExpressionHandler();
return oAuth2MethodSecurityExpressionHandler;
}
}

/**
* 当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PreAuthorize可以使用
* @PreAuthorize可以用来控制一个方法是否能够被调用。
* @在controller层方法中添加权限配置(符合条件得才可以调用这个方法)
* @return
*/
@PreAuthorize(value = "#oauth2.hasAnyScope(‘A‘,‘B‘,‘C‘,‘D‘)")//添加机构编码权限,判断该机构是否有权限调用
@PreAuthorize(value="isAuthenticated()")//添加登录权限判断,登录才可以调用
public String getInformation(){
return info;
}

原文地址:https://www.cnblogs.com/wueryuan/p/11401479.html

时间: 2024-08-28 15:02:04

spring security中@PreAuthorize注解的使用的相关文章

Spring框架中利用注解进行自动装配的环境配置步骤和常见问题

第1步:配置XML文件 ,如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.spring

Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx":意思是返回到某个JSP页面上 如果想在此Action中跳转到另一个Action中怎样做呢? return "redirect://.do?" 例如: @RequestMapping(params = "action=delete") public String del

spring security中配置密码为md5的带salt加密

spring security中配置密码为md5的带salt加密 service: private Md5PasswordEncoder encoder; //spring security md5 public Md5PasswordEncoder getEncoder() { return encoder; } @Resource public void setEncoder(Md5PasswordEncoder encoder) { this.encoder = encoder; } @O

Spring Security中html页面设置hasRole无效的问题

Spring Security中html页面设置hasRole无效的问题 一.前言 学了几天的spring Security,偶然发现的hasRole和hasAnyAuthority的区别.当然,可能有更深入的我没看到.现在把我实际的调试结果分享给大家 二.失效原因及解决 下面是部分示例html页面代码 <div sec:authorize="hasRole('R_ADMIN')">   <p class="bg-info">权限0001可见

spring boot跨域请求访问配置以及spring security中配置失效的原理解析

一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[origin]就是协议.域名和端口号.例如:http://www.baidu.com:80这个URL. 什么是同源 若地址里面的协议.域名和端口号均相同则属于同源. 是否是同源的判断 例如判断下面的URL是否与 http://www.a.com/test/index.html 同源 http://www.a

Spring Boot中的注解

文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了解决EJB等大型企业框架对应用程序的侵入性,因此大量依靠配置文件来“非侵入式”得给POJO增加功能,然而,从Spring 3.x开始,Spring被外界最为诟病的一点就是配置繁多,号称“配置地狱”,各种xml文件,出了问题非常难排查.从Spring 4.x开始,Spring.io提供了三种方式编织B

Spring MVC中基于注解的 Controller

终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响应请求.实际上,ControllerClassNameHandlerMapping, MultiActionController 和选择恰当的 methodNameResolver(如 InternalPathMethodNameResolver) 就已经可以在很大程度上帮助我们省去不少的 XML 配置,谁让

详解Java的Spring框架中的注解的用法

转载:http://www.jb51.net/article/75460.htm 1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的 类的实现: class UserManagerImpl implements UserManager { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } ... } 配置文件: <bean id=&qu

Spring Cloud中的注解

一.Eureka @EnableEurekaServer: @EnableDiscoverClient:标注服务是一个Eureka的客户端 @LoadBalanced:自动构造LoadBalancerClient接口的实现类并注册到Spring容器中 二.Feign @EnableFeignClients:开启Feign @FeignClient:标识为Feign客户端 三.Hystix @EnableHystix:开启Hystix @HystixCommand:指定依赖的服务失败时执行的方法