安全相关及HttpClient

1,Spring Security入门示例

Spring Security Annotation Configuration Example – HelloWorld

详细介绍spring security 结合RBAC

Springboot + Spring Security 实现前后端分离登录认证及权限控制

2,程序模块Get请求,获取响应

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils; String address = "http://localhost:8081/admin";
String authorization = "Authorization";
String baseCode = "Basic dGVzdDp0ZXN0"; HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(address);
request.addHeader(authorization, baseCode);
HttpResponse response = client.execute(request);
return EntityUtils.toString(response.getEntity());

另一种方法

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method); return method.getResponseBodyAsString();

3, 示例用OAuth2保护Rest API

http://lxgandlz.cn/403.html, 亲测有效,注意获取token时用post方式,并且注意还要配置基本认证帐号为my-trusted-client(注意看postman上切图)

上一篇:《MFC游戏开发》笔记五 定时器和简单动画


下一篇:Server from URL