ios或者安卓在登入的时候为了统一校验,一般的的情况下会到一个共同的接口进行检验,这里用java做了一个简单的后台接口用于ios或者安卓登入校验。后台的java框架是SSM框架。
下边是校验的流程:
使用的maven,以下是pom.xml:
4.0.0
com.demo
login-mobile
1.0.0-SNAPSHOT
war
org.springframework
spring-context
4.2.5.RELEASE
org.springframework
spring-beans
4.2.5.RELEASE
org.springframework
spring-webmvc
4.2.5.RELEASE
org.springframework
spring-jdbc
4.2.5.RELEASE
org.springframework
spring-aspects
4.2.5.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.7.5
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.2
com.github.pagehelper
pagehelper
4.1.6
com.github.jsqlparser
jsqlparser
0.9.5
com.github.abel533
mapper
2.3.4
org.springframework.ldap
spring-ldap-core
2.1.0.RELEASE
org.slf4j
slf4j-api
1.7.12
org.slf4j
slf4j-log4j12
1.7.12
log4j
log4j
1.2.17
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet
jsp-api
2.0
provided
mysql
mysql-connector-java
5.1.30
c3p0
c3p0
0.9.1.2
org.apache.commons
commons-lang3
3.4
junit
junit
4.12
org.springframework
spring-test
4.2.5.RELEASE
login
webAppRootKey
login
log4jConfigLocation
classpath:properties/log4j.properties
org.springframework.web.util.Log4jConfigListener
contextConfigLocation
classpath:spring/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF8
encodingFilter
/*
login
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springMVC-servlet.xml
1
login
/
login.jsp
package com.demo.login.pojo;
public class User {
//id
private Long id;
//user_id
private String userId;
private String passWord;
//token
private String token;
//device_id
private String deviceId;
//msg_uid
private String msgUid;
//version
private String version;
//token_date
private String tokenDate;
//os_type
private String osType;
public String getOsType() {
return osType;
}
public void setOsType(String osType) {
this.osType = osType;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getMsgUid() {
return msgUid;
}
public void setMsgUid(String msgUid) {
this.msgUid = msgUid;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getTokenDate() {
return tokenDate;
}
public void setTokenDate(String tokenDate) {
this.tokenDate = tokenDate;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public String toString() {
return "User [id=" + id + ", userId=" + userId + ", token=" + token + ", deviceId=" + deviceId + ", msgUid="
+ msgUid + ", version=" + version + ", tokenDate=" + tokenDate + ", osType=" + osType + "]";
}
}
package com.demo.login.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo.login.pojo.User;
import com.demo.login.service.LoginService;
@RestController
@RequestMapping(value="login")
public class LoginController {
@Autowired
public LoginService loginService;
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Map login(User user){
Map map = new HashMap();
try {
map = loginService.login(user);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
service层的实现类:
package com.demo.service.login.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.login.mapper.LoginMapper;
import com.demo.login.pojo.User;
import com.demo.login.service.LoginService;
import com.demo.login.util.CodeStatus;
import com.demo.login.util.SystemStatus;
import com.demo.login.util.UUIDUtil;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
public LoginMapper loginMapper;
@Override
public Map login(User user) throws Exception {
//登入校验返回的数据
Map map = new HashMap();
//校验数据
if (user != null) {
if (StringUtils.isBlank(user.getDeviceId())) {
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
map.put("data", "");
map.put("msg", "deviceId字段不能为空");
return map;
}
if (StringUtils.isBlank(user.getMsgUid())) {
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
map.put("data", "");
map.put("msg", "msgUid字段不能为空");
return map;
}
if (StringUtils.isBlank(user.getVersion())) {
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
map.put("data", "");
map.put("msg", "version字段不能为空");
return map;
}
if (StringUtils.isBlank(user.getOsType())) {
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
map.put("data", "");
map.put("msg", "osType字段不能为空");
return map;
}
}else{
return map;
}
//1.1token登入
int tokenCount = loginMapper.validateByToken(user);
if (tokenCount == 1) {
//若token校验成功,则登入成功
map.put("code", CodeStatus.SUCCESS);
map.put("status", SystemStatus.SUCCESS);
//TODO 这里data可以获取登入返回的数据,比如权限数据等
map.put("data", "this login by token");
map.put("msg", "token校验成功");
return map;
}else{//1.2账号和密码登入
//1.2.1校验,可以用其他的方式校验,比如ldap
int userIdCount = 0;
if (StringUtils.isNoneBlank(user.getPassWord()) && StringUtils.isNoneBlank(user.getUserId())) {
userIdCount = loginMapper.login(user);
}else{
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
map.put("data", "");
map.put("msg", "用户名密码字段不能为空");
return map;
}
if (userIdCount == 1) {//校验成功
//token表中插入或更新数据
user.setToken(UUIDUtil.getUuid());
loginMapper.insertOrUpdate(user);
map.put("code", CodeStatus.SUCCESS);
map.put("status", SystemStatus.SUCCESS);
//TODO 这里data可以获取登入返回的数据,比如权限数据等
map.put("data", "this login by token");
map.put("msg", "账号和密码校验成功");
return map;
}else{
map.put("code", CodeStatus.FAIL);
map.put("status", SystemStatus.SUCCESS);
//TODO 这里data可以获取登入返回的数据,比如权限数据等
map.put("data", "this login by userId and password");
map.put("msg", "账号或密码校验失败");
return map;
}
}
}
}
INSERT INTO
mobile_login(user_id,token,device_id,msg_uid,version,token_date,os_type)
VALUES
(
#{userId}
,#{token}
,#{deviceId}
,#{msgUid}
,#{version}
,date_add(NOW()
,interval 10080 minute)
,#{osType}
) ON DUPLICATE KEY UPDATE
token = #{token}
,device_id = #{deviceId}
,msg_uid = #{msgUid}
,version = #{version}
,os_type = #{osType}
DROP TABLE IF EXISTS `mobile_login`;
CREATE TABLE `mobile_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` char(20) NOT NULL,
`token` varchar(50) DEFAULT NULL,
`device_id` varchar(100) DEFAULT NULL,
`msg_uid` varchar(100) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`token_date` date DEFAULT NULL,
`os_type` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id_unique` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` char(50) DEFAULT NULL,
`user_name` varchar(50) DEFAULT NULL,
`pass_word` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
关键的代码都在这上边了,感兴趣的小伙伴可以观看我录制的视频:
http://v.youku.com/v_show/id_XMzMxNzc1NDM5Mg==.html?spm=a2h3j.8428770.3416059.1