完成用户登录、鉴权、获取用户信息
This commit is contained in:
parent
6b2ef02919
commit
cfc6c69613
|
|
@ -84,12 +84,6 @@
|
|||
<artifactId>easyexcel</artifactId>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.7.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ package com.ruoyi.business.ajax;
|
|||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.business.domain.BizMember;
|
||||
import com.ruoyi.business.model.Member;
|
||||
import com.ruoyi.business.service.IBizMemberService;
|
||||
import com.ruoyi.business.utils.Encrypt;
|
||||
import com.ruoyi.business.utils.JWTUtil;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.JWTUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
|
@ -33,27 +33,24 @@ public class AjaxLoginController extends BaseController {
|
|||
return AjaxResult.warn("请输入用户名密码");
|
||||
}
|
||||
|
||||
BizMember member = bizMemberService.selectBizMemberByMobile(mobile);
|
||||
if (Objects.isNull(member)) {
|
||||
BizMember bizMember = bizMemberService.selectBizMemberByMobile(mobile);
|
||||
if (Objects.isNull(bizMember)) {
|
||||
return AjaxResult.warn("用户名或密码错误");
|
||||
}
|
||||
// DES加密
|
||||
String encryptPassword = Encrypt.encrypt(password);
|
||||
if (!encryptPassword.equals(member.getPassword())) {
|
||||
if (!encryptPassword.equals(bizMember.getPassword())) {
|
||||
return AjaxResult.warn("用户名或密码错误");
|
||||
}
|
||||
|
||||
if (member.getIsEnable() == 0) {
|
||||
if (bizMember.getIsEnable() == 0) {
|
||||
return AjaxResult.warn("账户已禁用,请联系系统管理员");
|
||||
}
|
||||
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("id", member.getId());
|
||||
object.put("name", member.getMemberName());
|
||||
object.put("mobile", member.getMobile());
|
||||
Member member = new Member(bizMember.getId(), bizMember.getMemberName(), bizMember.getMobile());
|
||||
|
||||
Long day = 1000L * 60L * 60L;
|
||||
String token = JWTUtil.createJWT(object.toJSONString(), day);
|
||||
return super.success(token);
|
||||
String token = JWTUtil.createJWT(member.toJsonString(), day);
|
||||
return AjaxResult.success(token);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,9 @@ package com.ruoyi.business.ajax;
|
|||
|
||||
import com.ruoyi.business.domain.BizMember;
|
||||
import com.ruoyi.business.domain.BizMemberAddress;
|
||||
import com.ruoyi.business.domain.BizProduct;
|
||||
import com.ruoyi.business.service.IBizMemberAddressService;
|
||||
import com.ruoyi.business.service.IBizMemberService;
|
||||
import com.ruoyi.business.service.IBizProductService;
|
||||
import com.ruoyi.business.service.IBizProductTypeService;
|
||||
import com.ruoyi.common.annotation.AjaxLogin;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
|
@ -16,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@AjaxLogin
|
||||
@RestController
|
||||
@RequestMapping("/ajax/member")
|
||||
public class AjaxMemberController extends AuthController {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,24 @@
|
|||
package com.ruoyi.business.ajax;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.business.model.Member;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
public class AuthController extends BaseController {
|
||||
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
|
||||
//获取前端登录用户ID
|
||||
public Long getUserID()
|
||||
{
|
||||
return 1L;
|
||||
public Long getUserID() {
|
||||
return getMember().getId();
|
||||
}
|
||||
|
||||
public Member getMember() {
|
||||
String jsonString = (String) request.getAttribute("member");
|
||||
return JSONObject.parseObject(jsonString, Member.class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package com.ruoyi.business.model;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
/**
|
||||
* 用户实体
|
||||
* @author bei.wu
|
||||
*/
|
||||
public class Member {
|
||||
|
||||
/**
|
||||
* 用户主键
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 用户名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 用户手机号
|
||||
*/
|
||||
private String mobile;
|
||||
|
||||
public Member() {
|
||||
|
||||
}
|
||||
|
||||
public Member(Long id, String name, String mobile) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.mobile = mobile;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getMobile() {
|
||||
return mobile;
|
||||
}
|
||||
|
||||
public void setMobile(String mobile) {
|
||||
this.mobile = mobile;
|
||||
}
|
||||
|
||||
public String toJsonString() {
|
||||
return JSONObject.toJSONString(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -101,6 +101,11 @@
|
|||
<artifactId>javax.servlet-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.7.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -6,7 +6,7 @@ import java.lang.annotation.*;
|
|||
* 前端接口鉴权
|
||||
* @author bei.wu
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface AjaxLogin
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ruoyi.business.utils;
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.JwtBuilder;
|
||||
|
|
@ -1,10 +1,21 @@
|
|||
package com.ruoyi.framework.interceptor;
|
||||
|
||||
import com.ruoyi.common.annotation.AjaxLogin;
|
||||
import com.ruoyi.common.annotation.RepeatSubmit;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.json.JSON;
|
||||
import com.ruoyi.common.utils.JWTUtil;
|
||||
import com.ruoyi.common.utils.ServletUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* /ajax/**
|
||||
|
|
@ -16,6 +27,29 @@ public class AjaxAuthenticationInterceptor extends HandlerInterceptorAdapter {
|
|||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
if (handler instanceof HandlerMethod) {
|
||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||
AjaxLogin classAnnotation = ((HandlerMethod) handler).getMethod().getDeclaringClass().getAnnotation(AjaxLogin.class);
|
||||
AjaxLogin annotation = handlerMethod.getMethod().getAnnotation(AjaxLogin.class);
|
||||
if (ObjectUtils.anyNotNull(classAnnotation, annotation)) {
|
||||
String token = request.getHeader("Authorization");
|
||||
if (StringUtils.isBlank(token)) {
|
||||
AjaxResult ajaxResult = AjaxResult.error("请登录后操作");
|
||||
ServletUtils.renderString(response, JSON.marshal(ajaxResult));
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Claims claims = JWTUtil.parseJWT(token);
|
||||
request.setAttribute("member", claims.getSubject());
|
||||
} catch (Exception e) {
|
||||
AjaxResult ajaxResult = AjaxResult.error("没有权限");
|
||||
ServletUtils.renderString(response, JSON.marshal(ajaxResult));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return super.preHandle(request, response, handler);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue