134 lines
4.2 KiB
Java
134 lines
4.2 KiB
Java
package io.v.nutz.mobile;
|
|
|
|
import cn.wizzer.framework.base.Result;
|
|
import io.v.nutz.base.utils.LoginUtil;
|
|
import io.v.nutz.sys.models.Sys_user;
|
|
import io.v.nutz.sys.services.SysUserService;
|
|
import io.v.nutz.web.commons.base.Globals;
|
|
import io.v.nutz.web.commons.shiro.filter.PlatformAuthenticationFilter;
|
|
import io.v.nutz.web.commons.utils.ShiroUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
|
import org.apache.shiro.authc.IncorrectCredentialsException;
|
|
import org.apache.shiro.authc.UnknownAccountException;
|
|
import org.apache.shiro.authz.annotation.RequiresAuthentication;
|
|
import org.apache.shiro.session.SessionException;
|
|
import org.apache.shiro.subject.Subject;
|
|
import org.nutz.dao.Chain;
|
|
import org.nutz.dao.Cnd;
|
|
import org.nutz.ioc.loader.annotation.Inject;
|
|
import org.nutz.ioc.loader.annotation.IocBean;
|
|
import org.nutz.mvc.annotation.*;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
@IocBean
|
|
@At("/mobile/login")
|
|
@Ok("json")
|
|
@Slf4j
|
|
public class mobileLoginController {
|
|
|
|
@Inject
|
|
private LoginUtil loginUtil;
|
|
|
|
@Inject
|
|
private SysUserService sysUserService;
|
|
|
|
/**
|
|
* 登录页
|
|
*/
|
|
// @At("")
|
|
// @Ok("beetl:/mobile/login.html")
|
|
// public void index() {
|
|
//
|
|
// }
|
|
|
|
/**
|
|
* 首页
|
|
*/
|
|
@At(value = {"/mobile/home", "/mobile/index"}, top = true)
|
|
@Ok("beetl:/mobile/newHome.html")
|
|
@RequiresAuthentication
|
|
public void home() {
|
|
|
|
}
|
|
|
|
@At(value = "/mobile/index/toHome", top = true)
|
|
@Ok("beetl:/mobile/home.html")
|
|
@RequiresAuthentication
|
|
public void toHome() {
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 登录
|
|
*
|
|
* @param request request
|
|
* @param session 会话
|
|
* @return {@link Object}
|
|
*/
|
|
@At("/doLogin")
|
|
@Ok("json")
|
|
@Filters(@By(type = PlatformAuthenticationFilter.class))
|
|
public Object doLogin(@Attr("platformLoginToken") AuthenticationToken token, HttpServletRequest request, HttpSession session) {
|
|
try {
|
|
loginUtil.doLogin(token, request, session, LoginUtil.LoginOrigin.WEB_H5);
|
|
return Result.success("login.success");
|
|
} catch (IncorrectCredentialsException | UnknownAccountException e) {
|
|
return Result.error(4, "login.error.user");
|
|
} catch (AuthenticationException e) {
|
|
return Result.error(5, "login.error.user");
|
|
} catch (Exception e) {
|
|
return Result.error(6, "login.error.system");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 退出系统
|
|
*/
|
|
@At
|
|
@Ok(">>:/mobile/login")
|
|
public void out(HttpSession session, HttpServletRequest req) {
|
|
try {
|
|
Subject currentUser = SecurityUtils.getSubject();
|
|
Sys_user user = (Sys_user) currentUser.getPrincipal();
|
|
currentUser.logout();
|
|
session.setAttribute("currentUser", null);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解绑微信openId
|
|
*
|
|
* @return
|
|
*/
|
|
@At
|
|
@Ok("re")
|
|
public String clearWxOpenId() {
|
|
try {
|
|
sysUserService.update(Chain.make("openId", null), Cnd.where("id", "=", ShiroUtil.getPrincipalProperty("id")));
|
|
Subject subject = SecurityUtils.getSubject();
|
|
Sys_user user = (Sys_user) subject.getPrincipal();
|
|
subject.logout();
|
|
sysUserService.deleteCache(user.getId());
|
|
return ">>:" + Globals.CasAddress + "/logout?service=" + URLEncoder.encode(Globals.AppDomain + "/platform/wechat/oauth", StandardCharsets.UTF_8);
|
|
} catch (SessionException ise) {
|
|
log.debug("Encountered session exception during logout. This can generally safely be ignored.", ise);
|
|
return ">>:" + Globals.CasAddress + "/logout?service=" + URLEncoder.encode(Globals.AppDomain + "/platform/wechat/oauth", StandardCharsets.UTF_8);
|
|
} catch (Exception e) {
|
|
log.debug("Logout error", e);
|
|
return ">>:" + Globals.CasAddress + "/logout?service=" + URLEncoder.encode(Globals.AppDomain + "/platform/wechat/oauth", StandardCharsets.UTF_8);
|
|
}
|
|
}
|
|
|
|
}
|