Unity&Springboot实现本地登陆验证

网友投稿 670 2022-12-25

Unity&Springboot实现本地登陆验证

Unity&Springboot实现本地登陆验证

目录Springboot使用IDEA编译器IDEA上实现登录验证返回登录是否成功和登陆用户的id信息Unity端的请求

Springboot使用IDEA编译器

IDEA上实现登录验证

因为这里只能返回网页,但是我们需要返回登陆是否成功的数据所以下面还需要写一个请求方法

如果登陆失败则将session域中的id删除,这样在unity判断是否登录成功时会直接按请求错误抓取

//登录操作

@RequestMapping("/login")

public String login(HttpServletRequest request, @RequestParam("userType") String userType, Map map,HttpSession session) {

session.setAttribute("id",request.getParameter("id"));

String id = session.getAttribute("id").toString();

String password = request.getParameter("password");

//如果是管理员登录则查询管理员信息表

if(userType.equals("0")){

Administrators administrator = administratorsService.login(id, password);

if(administrator != null){

System.out.println("登陆成功");

return "redirect:/ScheduleInfo";

}else {

map.put("msg","账号或密码错误");

//如果登陆失败则将session域中的id删除,这样在unity判断是否登录成功时会直接按请求错误抓取

session.removeAttribute("id");

return "login";

}

}else { //如果是普通用户登录则查找普通用户表

Employees employee = employeesService.login(id, password);

if(employee != null){

if (employeesService.findJobById(id).getJob().equals("巡检人员")){

System.out.println("登陆成功");

return "redirect:/xInfo";

}else {

System.out.println("登陆成功");

return "redirect:/wInfo";

}

}else {

map.put("msg","账号或密码错误");

session.removeAttribute("id");

return "login";

}

}

}

返回登录是否成功和登陆用户的id信息

这里使用 @ResponseBody注解,使返回的是数据而不是网页

@RequestMapping("/getUserInfo")

@ResponseBody

public String getUserInfo(HttpSession session){

System.out.println("收到unity登录请求");

//因为登陆失败以后session域中的id会被删除,所以判断为null则登录失败

if(session.getAttribute("id") != null){

String id = session.getAttribute("id").toString();

System.out.println("登陆成功");

return id ;

}

else {

SFIAzi System.out.println("登陆失败");

return null;

}

}

Unity端的请求

一个简单的登陆注册界面

上脚本,看注释

using System.Collections;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

using UnityEngine.Networking;

public class HttpHelper : MonoBehaviour

{

//发出登录请求

private string postUrl = "http://47.xx.75.xx:8080/login";//如果是本地运行则将前面的47.96.75.29换成localhost

//获得登录是否成功的数据,也就是运行上面第二个代码的内瓤

private string postUrl2 = "http://47.xx.75.xx:8080/getUserInfo";

public GameObject[] uis;

public GameObject backLoginObj;

public Text massage;

public Text countText;

public Text passwordText;

private const string userType = "userType";

private const string userName = "id";

private const string password = "password";

public void loginTest()

{

//这个方法和登录按钮绑定,用于触发异步方法Post

StartCoroutine("Post");

}

[System.Obsolete]

IEnumerator Post()

{

//发送登录表单,每个人不一样,根据自己需要的表单参数来,一般就是账号密码,这里的userType就是管理员和员工的分类,0是管理员,1是员工。

WWWForm form = new WWWForm();

form.AddField(userType, "0");

form.AddField(userName, countText.text);

form.AddField(password, passwordText.text);

//这里发出了登录请求

//利用UnityWebRequest通过请求路径这个和postman的操作类似,将表单发送出去

UnityWebRequest request = UnityWebRequest.Post(postUrl, form);

yield return request.SendWebRequest();

if (request.isHttpError || request.isNetworkError)

{

Debug.LogError(request.error);

}

//这里获取了登录是否成功的数据

UnityWebRequest rSFIAziequest2 = UnityWebRequest.Get(postUrl2);

yield return request2.SendWebRequest();

//如果登陆失败的Session域中的id是空的,所以会报错,也就是判断登陆是否成功的依据。

if (request2.isHttpError || request2.isNetworkError)

{

massage.text = "登陆失败,账号或密码错误";

}

else

{

//反之如果登录成功则获得返回的数据,这里就是用户的id

string receiveContent = request2.downloadHandler.text;

//这是个普通的ui操作,我的构想是如果登录成功则将这些ui隐藏只显示massage和一个返回键

foreach (GameObject ui in uis)

{

ui.SetActive(false);

http://http:// }

massage.gameObject.SetActive(true);

backLoginObj.SetActive(true);

//如果返回的数据和用户输入时的账号一样时则判断登陆成功

if (receiveContent == countText.text)

{

massage.text = "登陆成功,欢迎管理员" + receiveContent;

}

else//反之登陆失败

{

massage.text = "登陆失败,账号或密码错误";

}

}

StopCoroutine("Post");

}

public void backLogin()

{

SceneManager.LoadScene("SampleScene");

}

}

最后的运行结果

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:flutter背景颜色(flutter设置背景图片)
下一篇:智能车载终端品牌排名(智能驾驶供应商排名)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~