资源服务令牌解析配置package com.jt.resource.config;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.web.access.AccessDeniedHandler;import javax.servlet.http.HttpServletResponse;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;@Configuration@EnableResourceServer@EnableGlobalMethodSecurity(prePostEnabled = true)public class ResourceServerConfig extends ResourceServerConfigurerAdapter {@Autowiredprivate TokenStore tokenStore;/*** token服务配置*/@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {resources.tokenStore(tokenStore);}/*** 路由安全认证配置*/@Overridepublic void configure(HttpSecurity http) throws Exception {http.csrf().disable();http.exceptionHandling().accessDeniedHandler(accessDeniedHandler());http.authorizeRequests().anyRequest().permitAll();}//没有权限时执行此处理器方法public AccessDeniedHandler accessDeniedHandler() {return (request, response, e) -> {Map<String, Object> map = new HashMap<>();map.put("state", HttpServletResponse.SC_FORBIDDEN);//SC_FORBIDDEN的值是403map.put("message", "没有访问权限,请联系管理员");//1设置响应数据的编码response.setCharacterEncoding("utf-8");//2告诉浏览器响应数据的内容类型以及编码response.setContentType("application/json;charset=utf-8");//3获取输出流对象PrintWriter out=response.getWriter();//4 输出数据String result=new ObjectMapper().writeValueAsString(map);out.println(result);out.flush();};}}ResourceController 方法配置在controller的上传方法上添加 @PreAuthorize(“hasAuthority(‘sys:res:create’)”)注解,用于告诉底层框架方法此方法需要具备的权限,例如
@PreAuthorize("hasAuthority('sys:res:create')")@PostMapping("/upload/")public String uploadFile(MultipartFile uploadFile) throws IOException {...}启动服务访问测试
- 第一步:启动服务(sca-auth,sca-resource-gateway,sca-resource)
- 第二步:执行登陆获取access_token令牌
- 第三步:携带令牌访问资源(url中的前缀”sca”是在资源服务器中自己指定的,你的网关怎么配置的,你就怎么写)

设置请求体(body),设置form-data,key要求为file类型,参数名与你服务端controller文件上传方法的参数名相同,值为你选择的文件,例如

上传成功会显示你访问文件需要的路径,假如没有权限会提示你没有访问权限 。
文件上传JS方法设计
function upload(file){//定义一个表单let form=new FormData();//将图片添加到表单中form.append("uploadFile",file);let url="http://localhost:9000/sca/resource/upload/";//异步提交方式1axios.post(url,form,{headers:{"Authorization":"Bearer "+localStorage.getItem("accessToken")}}).then(function (response){let result=response.data;if(result.state==403){alert(result.message);return;}alert("upload ok");})}技术摘要应用实践说明背景分析企业中数据是最重要的资源,对于这些数据而言,有些可以直接匿名访问,有些只能登录以后才能访问,还有一些你登录成功以后,权限不够也不能访问.总之这些规则都是保护系统资源不被破坏的一种手段.几乎每个系统中都需要这样的措施对数据(资源)进行保护.我们通常会通过软件技术对这样业务进行具体的设计和实现.早期没有统一的标准,每个系统都有自己独立的设计实现,但是对于这个业务又是一个共性,后续市场上就基于共性做了具体的落地实现,例如Spring Security,Apache shiro,JWT,Oauth2等技术诞生了.
推荐阅读
- 路由器登录密码是什么 路由器的默认密码一般是多少?
- 手机wifi无ip分配解决方法 电脑ip配置失败是什么原因
- 国内手机登录facebook技巧 微信如何登入facebook账号
- 免费邮箱163 @163.com邮箱怎么登录?
- 喋血复仇游戏打不开怎么办 喋血复仇启动失败解决办法
- 迷你世界怎样玩测试服 怎么下载和登录呢
- 和平精英怎么登录 具体步骤分享
- 隐形守护者第二章潜伏失败怎么办 第二章潜伏方法分享_网
- 隐形守护者网络异常是什么情况 网络链接失败解决方法分享
- 新射雕群侠传之铁血丹心每日登录活动攻略 9月18日每日登录活动介绍
