本文章是利用restful接口调用登录接口
一、登录接口实现返回token
(1)controller层
//登录接口
@RequestMapping(value = "/selectUserInFo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
@ApiOperation(value = "登录接口", notes = "登录接口")
//@requestBody表示传入的是对象,传入的参数格式需要以对象的格式
public ResponseEntity<UserInFo> selectUser(@RequestBody LoginParam loginParam) {
try {
//调用service的方法,得到用户,我将token封装进UserInFo对象了
UserInFo user= service.selectUserInFo(loginParam.getUsername(),loginParam.getUserpassword());
return ResponseEntity.success(user);
} catch (Exception e) {
e.printStackTrace();
logger.error("selectUser", e);
return ResponseEntity.fail(e);
}
}
}
(2)service层
//登录接口
@Override
public UserInFo selectUserInFo(String username, String userpassword) throws UserInFoNotLookupException {
//先查询数据库是否有登录的用户名与密码是否正确
UserInFo user =dao.selectUserInFo(username,userpassword);
String token=null;
//判断是否有该用户
if(user==null) {
throw new UserInFoNotLookupException("用户名或密码错误");
}else {
//有该用户则利用user对象生成token,后面数字是token过期时间
token =JwtUtils.encode(user,7200000);
//token封装进user对象
user.setToken(token);
}
//返回对象
return user;
}
(3)dao层我就不写了,每个人dao层都不一样,我用的是mybatis,后面我会写一点关于mybatis的操作
二、在其他接口判断token,不正确不给调用接口
//查询建筑数据
@RequestMapping(value = "/toselectArchitecture", method = RequestMethod.GET)
@ApiOperation(value = "查询建筑数据", notes = "查询建筑数据")
public ResponseEntity<Architecture> selectArchitecture(HttpServletRequest request,String regionid) {
try {
//通过url请求头得到token
String token = request.getHeader("token");
//检测token是否为空
if(token==null) {
throw new NotloggedInException("用户未登录");
}else {
//token不为空检查是否过期或不正确
UserInFo user=JwtUtils.decode(token,UserInFo.class);
if(user==null) {
throw new NotloggedInException("用户未登录");
}
}
//token检查通过后再进行操作
Architecture architecture=service.selectArchitecture(regionid);
return ResponseEntity.success(architecture);
} catch (Exception e) {
e.printStackTrace();
//如果有错误则返回错误信息
return ResponseEntity.fail(e);
}
}
jwtutils
public class JwtUtils { /** * 密钥 */ private static final String SECRET="xxxx"; /** * 默认字段key:exp */ private static final String EXP="exp"; /** * 默认字段key:payload */ private static final String PAYLOAD="payload"; /** * 加密 * @param object 加密数据 * @param maxTime 有效期(毫秒数) * @param <T> * @return */ public static <T> String encode(T object,long maxTime){ try{ final JWTSigner signer=new JWTSigner(SECRET); final Map<String ,Object> data=new HashMap<>(10); ObjectMapper objectMapper=new ObjectMapper(); String jsonString=objectMapper.writeValueAsString(object); data.put(PAYLOAD,jsonString); data.put(EXP,System.currentTimeMillis()+maxTime); return signer.sign(data); } catch (IOException e) { e.printStackTrace(); return null; } } /** * 数据解密 * @param jwt 解密数据 * @param tClass 解密类型 * @param <T> * @return * @throws Exception */ public static <T> T decode(String jwt,Class<T> tClass) throws Exception{ final JWTVerifier jwtVerifier=new JWTVerifier(SECRET); final Map<String,Object> data=jwtVerifier.verify(jwt); //判断数据是否超时或者符合标准 if(data.containsKey(EXP)&&data.containsKey(PAYLOAD)){ long exp= (long) data.get(EXP); long currentTimeMillis=System.currentTimeMillis(); if(exp>currentTimeMillis){ String json= (String) data.get(PAYLOAD); ObjectMapper objectMapper=new ObjectMapper(); return objectMapper.readValue(json,tClass); }else { throw new UserTimeoutException("用户登录超时"); } }else { throw new UserErrorTokenException("用户token错误"); } } }