Skip to content

Commit

Permalink
wlan
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiesun committed May 19, 2019
1 parent abe5537 commit 87f75f4
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/toughradius/config/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface Constant {

public final static String WLAN_MODULE = "wlan";
public final static String WLAN_TEMPLATE = "wlanTemplate";
public final static String WLAN_RESULT_URL = "wlanResultUrl";
public final static String WLAN_JOIN_URL = "wlanJoinUrl";
public final static String WLAN_USERAUTH_ENABLED = "wlanUserauthEnabled";
public final static String WLAN_PWDAUTH_ENABLED = "wlanPwdauthEnabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public RestResult updateSmsConfig(SmsConfigForm form){
configService.updateConfig(new Config(SMS_MODULE,SMS_GATEWAY,form.getSmsGateway()));
configService.updateConfig(new Config(SMS_MODULE,SMS_APPID,form.getSmsAppid()));
configService.updateConfig(new Config(SMS_MODULE,SMS_APPKEY,form.getSmsAppkey()));
configService.updateConfig(new Config(SMS_MODULE,SMS_VCODE_TEMPLATE,form.getSmsVcodeTemplate()));
}catch(Exception e){
logger.error("update config error",e, Memarylogger.SYSTEM);
}
Expand All @@ -99,6 +100,7 @@ public RestResult updateWlanConfig(WlanCongigForm form){
configService.updateConfig(new Config(WLAN_MODULE,WLAN_WECHAT_SECRETKEY,form.getWlanWechatSecretkey()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_TEMPLATE,form.getWlanTemplate()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_JOIN_URL,form.getWlanJoinUrl()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_RESULT_URL,form.getWlanResultUrl()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_USERAUTH_ENABLED,form.getWlanUserauthEnabled()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_PWDAUTH_ENABLED,form.getWlanPwdauthEnabled()));
configService.updateConfig(new Config(WLAN_MODULE,WLAN_SMSAUTH_ENABLED,form.getWlanSmsauthEnabled()));
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/org/toughradius/controller/PortalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,25 @@ private String getWlanemplate(){
return template;
}

private void setResultUrl(WlanParam param){
String resultUrl = configService.getStringValue(WLAN_MODULE,WLAN_RESULT_URL);
if(ValidateUtil.isNotEmpty(resultUrl)){
param.setWlanuserfirsturl(resultUrl);
}
}

/**
* portal 首页
* @param wlanParam
* @return
*/
@GetMapping("/wlan/index")
public ModelAndView wlanIndexHandler(WlanParam wlanParam){

String template = getWlanemplate();
ModelAndView modelAndView = new ModelAndView(template+"/index");
wlanParam.setTemplate(template);
setResultUrl(wlanParam);
modelAndView.addObject("params", wlanParam);
setConfigModel(modelAndView);
return modelAndView;
Expand All @@ -114,6 +123,7 @@ public ModelAndView wlanIndexHandler(WlanParam wlanParam){
public ModelAndView wlanLoginHandler(WlanParam wlanParam){
ModelAndView modelAndView = new ModelAndView(getWlanemplate()+"/"+wlanParam.getAuthmode());
modelAndView.addObject("params", wlanParam);
setResultUrl(wlanParam);
return modelAndView;
}

Expand All @@ -140,13 +150,15 @@ private ModelAndView processModel(ModelAndView mv, String username, String code,
@PostMapping("/wlan/login")
public ModelAndView wlanLoginPostHandler(HttpSession session,HttpServletRequest request, WlanParam param, String password){
ModelAndView modelAndView = new ModelAndView(getWlanemplate()+"/result");
modelAndView.addObject("param",param);
// 预处理参数
if(ValidateUtil.isEmpty(param.getWlanuserfirsturl())){
if(ValidateUtil.isNotEmpty(param.getUrl())){
param.setWlanuserfirsturl(param.getUrl());
}
}
param.setSrcacip(request.getRemoteAddr());
setResultUrl(param);

// 查找 AC 设备
Bras nas = null;
Expand Down Expand Up @@ -191,12 +203,14 @@ public ModelAndView wlanLoginPostHandler(HttpSession session,HttpServletRequest
*/
private ModelAndView userPwdAuth(HttpSession session, HttpServletRequest request, WlanParam param, Bras nas,String password){
ModelAndView mv = new ModelAndView(getWlanemplate()+"/result");
mv.addObject("param",param);
if(ValidateUtil.isEmpty(param.getUsername())){
return processModel(mv,param.getUsername(), MODEL_FAIL,"帐号不能为空");
}
if(ValidateUtil.isEmpty(password)){
return processModel(mv,param.getUsername(), MODEL_FAIL,"密码不能为空");
}
setResultUrl(param);
return doPortalAuth(session, request, mv, param, nas, param.getUsername(), password);
}

Expand All @@ -211,12 +225,14 @@ private ModelAndView userPwdAuth(HttpSession session, HttpServletRequest request
*/
private ModelAndView passwordAuth(HttpSession session,HttpServletRequest request, WlanParam param,Bras nas, String password){
ModelAndView mv = new ModelAndView(getWlanemplate()+"/result");
mv.addObject("param",param);
if(ValidateUtil.isEmpty(password)){
return processModel(mv,param.getWlanusername(), MODEL_FAIL,"密码不能为空");
}
String username = "pu_"+ CoderUtil.random16str();
subscribeCache.createTempSubscribe(username,password,1);
param.setUsername(username);
setResultUrl(param);
return doPortalAuth(session, request, mv, param, nas, username, password);
}

Expand All @@ -230,6 +246,7 @@ private ModelAndView passwordAuth(HttpSession session,HttpServletRequest request
*/
private ModelAndView weixinAuth(HttpSession session, HttpServletRequest request, WlanParam param, Bras nas){
ModelAndView mv = new ModelAndView(getWlanemplate()+"/weixin");
mv.addObject("param",param);
String username = "wxu_"+ CoderUtil.random16str();
String password = CoderUtil.random16str();
subscribeCache.createTempSubscribe(username,password,1);
Expand All @@ -254,6 +271,7 @@ private ModelAndView weixinAuth(HttpSession session, HttpServletRequest request,
mv.addObject("mac",param.getWlanusermac());
mv.addObject("bssid",param.getWlanapmac());
mv.addObject("sign",sign);
setResultUrl(param);
return doPortalAuth(session, request, mv, param, nas, username, password);
}

Expand Down Expand Up @@ -289,8 +307,12 @@ public void wechatCallback(HttpServletRequest request,HttpServletResponse respon
*/
private ModelAndView smsAuth(HttpSession session,HttpServletRequest request, WlanParam param,Bras nas){
ModelAndView mv = new ModelAndView(getWlanemplate()+"/result");
mv.addObject("param",param);
String phone = param.getPhone();
String smscode = param.getSmscode();
if(ValidateUtil.isEmpty(phone)){
return processModel(mv,param.getPhone(),MODEL_FAIL,"手机号码不能为空");
}
if(ValidateUtil.isEmpty(smscode)){
return processModel(mv,param.getPhone(),MODEL_FAIL,"验证码不能为空");
}
Expand All @@ -303,7 +325,7 @@ private ModelAndView smsAuth(HttpSession session,HttpServletRequest request, Wla
smscodeCache.remove(phone);
return processModel(mv,param.getPhone(),MODEL_FAIL,"验证码已经过期");
}
if(smscode.equals(smscounter.getSmscode())){
if(!smscode.equals(smscounter.getSmscode())){
return processModel(mv,param.getPhone(),MODEL_FAIL,"验证码无效");
}

Expand All @@ -316,6 +338,7 @@ private ModelAndView smsAuth(HttpSession session,HttpServletRequest request, Wla
password = u.getPassword();
}
param.setUsername(username);
setResultUrl(param);
return doPortalAuth(session, request, mv, param, nas, username, password);
}

Expand All @@ -325,19 +348,28 @@ private ModelAndView smsAuth(HttpSession session,HttpServletRequest request, Wla
* @return
* @throws ServiceException
*/
@PostMapping("/wlan/sendsms")
@GetMapping("/wlan/sendsms")
@ResponseBody
public RestResult sendSms(String phone)throws ServiceException{
if(!ValidateUtil.isPhone(phone)){
return new RestResult(1,"无效的手机号码");
}
Map<String,Object> map=new HashMap<>();
if(smscodeCache.values().stream().anyMatch(x->x.getPhone().equals(phone)&&(new Date().getTime()-x.getSendtime())<60000)){
return new RestResult(1,"同一手机号60秒只能发送一次请求");
}
String vocdeTemplate = configService.getStringValue(SMS_MODULE,SMS_VCODE_TEMPLATE);
if(ValidateUtil.isEmpty(vocdeTemplate)){
return new RestResult(1,"没有配置短信验证码模板");
}
String vcode = String.valueOf(random.nextInt((999999 - 111111) + 1) + 111111);
int r = smsSender.sendQcloudSms(phone,"您本次的验证码是:"+vcode);
int r = smsSender.sendQcloudSms(phone,vocdeTemplate.replaceAll("<vcode>",vcode));
if(r==0){
smscodeCache.put(phone,new SmsSender.SmscodeCounter(phone,vcode));
logger.info(phone,"短信验证码发送成功: "+phone+" "+vcode);
return RestResult.SUCCESS;
}
logger.error(phone,"短信验证码发送成失败: "+phone+" "+vcode);
return RestResult.UNKNOW;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/toughradius/form/SmsConfigForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class SmsConfigForm {
private String smsGateway;
private String smsAppid;
private String smsAppkey;
private String smsVcodeTemplate;

public String getSmsGateway() {
return smsGateway;
Expand All @@ -29,4 +30,12 @@ public String getSmsAppkey() {
public void setSmsAppkey(String smsAppkey) {
this.smsAppkey = smsAppkey;
}

public String getSmsVcodeTemplate() {
return smsVcodeTemplate;
}

public void setSmsVcodeTemplate(String smsVcodeTemplate) {
this.smsVcodeTemplate = smsVcodeTemplate;
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/toughradius/form/WlanCongigForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class WlanCongigForm {
private String wlanWechatSecretkey;
private String wlanTemplate;
private String wlanJoinUrl;
private String wlanResultUrl;
private String wlanUserauthEnabled;
private String wlanPwdauthEnabled;
private String wlanSmsauthEnabled;
Expand Down Expand Up @@ -91,4 +92,12 @@ public String getWlanWxauthEnabled() {
public void setWlanWxauthEnabled(String wlanWxauthEnabled) {
this.wlanWxauthEnabled = wlanWxauthEnabled;
}

public String getWlanResultUrl() {
return wlanResultUrl;
}

public void setWlanResultUrl(String wlanResultUrl) {
this.wlanResultUrl = wlanResultUrl;
}
}
15 changes: 14 additions & 1 deletion src/main/resources/portal/default/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,32 @@
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100 p-t-90 p-b-30">
<#if code?? && code == "ok">
<form id="wlanForm" class="login100-form validate-form" method="get">
<span class="login100-form-title p-b-40">
认证成功
</span>
<div>
<a href="${wlanuserfirsturl!}" class="btn-login-with bg-green m-b-10" >
<a href="${param.wlanuserfirsturl!}" class="btn-login-with bg-green m-b-10" >
继续浏览网络
</a>
<a href="/wlan/disconnect" class="btn-login-with bg-red m-b-10">
断开网络
</a>
</div>
</form>
<#elseif code?? && code == "fail">
<form id="wlanForm" class="login100-form validate-form" method="get">
<span class="login100-form-message p-b-40">
认证失败 ${message!}
</span>
<div>
<a href="/wlan/disconnect" class="btn-login-with bg-red m-b-10">
返回
</a>
</div>
</form>
</#if>
</div>
</div>
</div>
Expand Down
60 changes: 44 additions & 16 deletions src/main/resources/portal/default/smsauth.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>ToughRADIUS Wlan Portal</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/portal/default/static/icons/favicon.ico"/>
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/animate/animate.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/css-hamburgers/hamburgers.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/animsition/css/animsition.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/select2/select2.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/daterangepicker/daterangepicker.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/css/util.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/css/main.css">
<title>ToughRADIUS Wlan Portal</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/portal/default/static/icons/favicon.ico"/>
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/animate/animate.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/css-hamburgers/hamburgers.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/animsition/css/animsition.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/select2/select2.min.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/vendor/daterangepicker/daterangepicker.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/css/util.css">
<link rel="stylesheet" type="text/css" href="/portal/default/static/css/main.css">
<link rel="stylesheet" type="text/css" href="/static/webix.css">
<script>
var countdown=60;

function sendsms(obj){
settime(obj)
var mobile = $("#phone").val();
webix.ajax().get('/wlan/sendsms',{phone: mobile}).then(function (result) {
var resp = result.json();
webix.message({type: resp.msgtype, text: resp.msg, expire: 1500});
});
}

function settime(obj) {
if (countdown === 0) {
obj.removeAttribute("disabled");
obj.value="获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {settime(obj) } ,1000)
}
</script>
</head>
<body>

Expand All @@ -26,7 +53,7 @@
<span class="txt1">用户认证</span>
</div>
<div class="wrap-input100 validate-input m-b-16" data-validate="请输入手机号码">
<input class="input100" type="text" autocomplete="false" name="phone" value="${params.phone!}" placeholder="手机号码">
<input class="input100" type="text" autocomplete="false" id="phone" name="phone" value="${params.phone!}" placeholder="手机号码">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input m-b-20" data-validate = "请输入验证码">
Expand All @@ -38,6 +65,7 @@
</div>

<div >
<input type="button" id="btn" value="获取验证码" onclick="sendsms(this)" class="btn-sms-with bg1 m-b-10" />
<a href='javascript:$("#wlanForm").submit();' class="btn-login-with bg-green m-b-10" >
认 证
</a>
Expand Down Expand Up @@ -66,7 +94,7 @@
</div>
</div>
</div>

<script src="/static/webix.js"></script>
<script src="/portal/default/static/vendor/jquery/jquery-3.2.1.min.js"></script>
<script src="/portal/default/static/vendor/animsition/js/animsition.min.js"></script>
<script src="/portal/default/static/vendor/bootstrap/js/popper.js"></script>
Expand Down
27 changes: 27 additions & 0 deletions src/main/resources/portal/default/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,33 @@ iframe {
}


.login100-form-message {
font-family: Oswald-Medium;
font-size: 20px;
color: #fcf4ea;
line-height: 1.2;
text-align: center;
display: block;
}

.btn-sms-with {
font-family: Oswald-Medium;
font-size: 16px;
color: #fff;
line-height: 1.2;

display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 50px;
border-radius: 25px;
}

/*------------------------------------------------------------------
[ Button Login with ]*/

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/static/admin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ toughradius.admin.config.loadPage = function(session){
rows:[
{view: "richselect", name: "wlanTemplate", label: "认证模板:",value:"default", options:[{id:"default",value:"默认"}]},
{view: "text", name: "wlanJoinUrl", label: "自助服务网站"},
{view: "text", name: "wlanResultUrl", label: "认证成功跳转"},
{view:"radio", name:"wlanUserauthEnabled", label: "用户密码认证", options:[{id:'enabled',value:"开启"}, {id:'disabled',value:"关闭"}]},
{view:"radio", name:"wlanPwdauthEnabled", label: "固定密码认证", options:[{id:'enabled',value:"开启"}, {id:'disabled',value:"关闭"}]},
{view:"radio", name:"wlanSmsauthEnabled", label: "手机短信认证", options:[{id:'enabled',value:"开启"}, {id:'disabled',value:"关闭"}]},
Expand Down
Loading

0 comments on commit 87f75f4

Please sign in to comment.