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")@ResponseBodypublic 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 {System.out.println("登陆失败"); return null; }}
Unity端的请求 【Unity&Springboot实现本地登陆验证】一个简单的登陆注册界面
文章图片
文章图片
上脚本,看注释
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(){//这个方法和登录按钮绑定,用于触发异步方法PostStartCoroutine("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 request2 = UnityWebRequest.Get(postUrl2); yield return request2.SendWebRequest(); //如果登陆失败的Session域中的id是空的,所以会报错,也就是判断登陆是否成功的依据。if (request2.isHttpError || request2.isNetworkError){massage.text = "登陆失败,账号或密码错误"; }else{//反之如果登录成功则获得返回的数据,这里就是用户的idstring receiveContent = request2.downloadHandler.text; //这是个普通的ui操作,我的构想是如果登录成功则将这些ui隐藏只显示massage和一个返回键foreach (GameObject ui in uis){ui.SetActive(false); }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"); }}
最后的运行结果
文章图片
文章图片
文章图片
到此这篇关于Unity&Springboot服务器/本地登陆验证的文章就介绍到这了,更多相关Unity&Springboot服务器/本地登陆验证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Activiti(一)SpringBoot2集成Activiti6
- 宋仲基&宋慧乔(我们不公布恋情,我们直接结婚。)
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 21天|21天|M&M《见识》04
- 二叉树路径节点关键值和等于目标值(LeetCode--112&LeetCode--113)
- Unity和Android通信系列文章2——扩展UnityPlayerActivity
- springboot使用redis缓存
- springboot整合数据库连接池-->druid