经过很长时间的准备,将项目中用到的一些比较实用的模块进行提取重构,释放出来与大家分享!
如果她对你有帮助,也希望不吝收藏、点赞!当然,请我们喝杯咖啡也是好滴嘛 :p!
更希望大家多多关注YMP开源项目,在不改变编码习惯的同时,以简单、直接的方式解决问题,还会带给你似曾相识的感觉,不妨试试看,也许你会对她说:“相见恨晚”!
期待与大家交流学习!
第一波集中发布如下模块:
YMP-OAuth-Connector:
第三方OAuth授权登录模块,目前已实现GitHub、Weibo、Baidu、Wechat、OSChina和QQ等;
示例说明:
首先,你需要创建基于YMP框架的Web工程项目;(如何快速搭建工程?)
申请服务帐号,本例申请的是GitHub第三方授权(访问https://github.com/settings/developers进行申请):
在申请过程中需要填写redirect_uri地址, 请填写:http://<你的域名>/oauth/connect/github/redirect;
在Web工程中配置OAuth模块参数:
# OAuth客户端ID, 必选项
ymp.configs.module.oauth.connector.github.client_id=<CLIENT_ID>
# OAuth客户端密钥, 必选项
ymp.configs.module.oauth.connector.github.client_secret=<CLIENT_SECRET>
最后,在浏览器地址栏输入如下URL进行测试:
http:
了解更多YMP-OAuth-Connector内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-oauth-connector
YMP-OAuth:
OAuth2授权服务模块;
支持客户端模式grant_type=client_credentials;
支持授权码模式grant_type=authorization_code;
支持密码模式grant_type=password;
支持scope权限范围:snsapi_base和snsapi_userinfo;
支持令牌更新:grant_type=refresh_token;
支持令牌有效性验证;
支持拦截器限制接口请求的scope权限:
示例代码:
通过拦截器限制接口请求scope的权限必须是snsapi_userinfo:
@RequestMapping("/sns/userinfo")
@Before(SnsAccessTokenCheckInterceptor.class)
@ContextParam(@ParamItem(key = IOAuth.Const.SCOPE, value = IOAuth.Scope.SNSAPI_USERINFO))
public IView userinfo(@RequestParam(IOAuth.Const.ACCESS_TOKEN) String accountToken, @RequestParam(IOAuth.Const.OPEN_ID) String openId) throws Exception {
try {
return View.jsonView(OAuth.get().getModuleCfg().getUserInfoAdapter().getUserInfo(OAuth.get().bindAccessResourceHelper(accountToken, openId).getOAuthClientUser().getUid()));
} catch (Exception e) {
OAuthResponse _response = __responseBadRequest(IOAuth.Const.INVALID_USER);
return new HttpStatusView(_response.getResponseStatus(), false).writeBody(_response.getBody());
}
}
客户端模式:
授权码模式:
获取授权码,以GET方式请求URL地址,成功则重定向并携带code授权码:
http:
接收到code授权码后,获取access_token令牌,以POST方式请求URL地址:
http:
POST请求报文:
POST /oauth2/sns/access_token HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
code=f32ab01222936356e5a8352b9beeacc3&client_id=default&client_secret=7890123&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth2%2Fsns%2Fredirect
密码模式:
令牌更新:
令牌有效性验证:
获取授权用户基本信息:
了解更多YMP-OAuth内容,请访问码云:https://git.oschina.net/suninformation/ymate-module-oauth
YMP-Captcha:
验证码模块;
示例代码:
验证码注解@VCaptcha的使用
@RequestMapping(value = "/login", method = Type.HttpMethod.POST)
public IView __doLogin(@VCaptcha(invalid = true)
@RequestParam String captcha,
@VRequried
@VMobile
@RequestParam String mobile,
@VRequried
@VCaptcha(tokenId = ICaptcha.Const.TOKEN_SMS)
@RequestParam String smscode,
@VRequried
@RequestParam String passwd,
@RequestParam(Optional.REDIRECT_URL) String redirectUrl) throws Exception {
return WebResult.SUCCESS().toJSON();
}
验证码相关方法调用
String _code = Captcha.get().generate("user.login");
Captcha.get().invalidate("user.login");
Captcha.get().isWrongTimesEnabled();
Captcha.get().isValidationNeedSkip("user.login");
Captcha.get().resetWrongTimes("user.login");
Captcha.get().validate("user.login", _code, true);
了解更多YMP-Captcha内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-captcha
YMP-SSO:
单点登录模块;
示例代码:
获取当前用户的登录授权令牌对象:
ISSOToken _token = SSO.get().currentToken();
使用单点登录拦截器:
@RequestMapping(value = "/user/profile/edit", method = Type.HttpMethod.POST)
@Before(UserSessionCheckInterceptor.class)
public IView __doEditUserProfile(@RequestParam String nickName, ......) throws Exception {
return WebResult.SUCCESS().toJSON();
}
了解更多YMP-SSO内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-sso
YMP-MailSender:
邮件发送服务模块,支持多帐号配置;
示例代码:
MailSender.get().create()
.to("notify@demo.xxx")
.cc("ceshi@demo.xxx")
.bcc("someone@demo.xxx")
.subject("帐户注册成功通知邮件")
.send("恭喜你...");
MailSender.get().create("demo")
.to("boss@demo.xxx")
.subject("匿名")
.send("再不开响,服务器格式化啦!");
了解更多YMP-MailSender内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-mailsender
YMP-FileUploader:
文件上传及资源访问服务模块;
示例代码:
http:
参数说明:
http:
参数说明:
{ret: 0, matched: true}
http:
参数说明:
注:若需要强制浏览器下载资源,只需在请求参数中添加?attach即可;
了解更多YMP-FileUploader内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-fileuploader
YMP-WebProxy:
简单HTTP请求透传代理模块,用于将本地请求转发至远程服务器并返回远程服务的响应结果;
支持透传请求头黑、白名单过滤;
支持请求黑名单过滤;
支持响应头白名单过滤;
了解更多YMP-WebProxy内容,请访问码云:http://git.oschina.net/suninformation/ymate-module-webproxy
One More Thing
YMP不仅提供便捷的Web及其它Java项目的快速开发体验,也将不断提供更多丰富的项目实践经验。
了解更多有关YMP框架的内容,请访问官网:http://www.ymate.net/