In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to imitate the token mechanism under J2EE. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Now many mobile applications need to interact with the background, and the interaction with the background requires identity authentication. When I first thought of using token, I used a parameter like token when I saw the identity authentication of Wechat public platform, and then I realized one by myself.
When it comes to this token mechanism, one is the calling side and the other is the background. On the calling side, I use the Android application developed by Java to show, and the background uses J2EE to show.
I. acquisition of token
The acquisition of token is actually the login process of the mobile end. When logging in, you need to obtain a token value from the backend. First, let's take a look at the code of Android when logging in:
String name = (EditText) mName.getText (). ToString (); String password = (EditText) mPsw.getText (). ToString (); String url2 = "http://"+ip+":"+port+"/test/be/login.do?method=login";OkHttpClientManager ohm = OkHttpClientManager.getInstance (); OkHttpClientManager.Param [] params = new OkHttpClientManager.Param [] {new OkHttpClientManager.Param (" name ", name), new OkHttpClientManager.Param (" password ", password)} OkHttpClientManager.ResultCallback cb = new OkHttpClientManager.ResultCallback () {@ Override public void onError (Request request, Exception e) {String resultMsg = "request failed, please check the network connection!" ; ToastUtil.showToastMsgError (LoginActivity.this, resultMsg); cancleAnimation (); / close the login wait box} @ Override public void onResponse (String response) {JSONObject returnJson = JSONObject.parseObject (response); String resultId = returnJson.get ("resultId") .toString () If (resultId.equals ("00")) {token = returnJson.get ("token") .toString (); System.out.println ("= = token:" + token); startApp () / / enter the application} else {token = ""; String resultMsg = returnJson.get ("resultMsg"). ToString (); ToastUtil.showToastMsgError (NewLoginActivity.this, resultMsg); cancleAnimation () OkHttpClientManager.getInstance (). PostAsyn (url2,cb, params)
OkHttpClientManager is an encapsulated OkHttp call class, which can be called directly to initiate a http request to the background.
Then let's take a look at the processing at the backend. There are several points to note here: first, the login interface must not be blocked by interceptors and filters, then the user name and password must be transferred to the ctrl layer, and then verify the true validity of the user name and password. If the identity is appropriate, return token. If there is a problem with the identity, return the corresponding prompt.
@ RequestMapping (params= "method=login") @ ResponseBodypublic String login (HttpServletRequest request,HttpServletResponse response,SysUser command) throws Exception {String resStr = "; try {String name = command.getName (); String password = command.getPassword (); UserSession userSession=new UserSession (); / / set usersession login mode userSession.setDlms (" 1 "); command.setDlms (" 1 "); SysUser sysuser=new SysUser () Department department=new Department (); if (name==null | | name.length ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.