iOS怎么通过取Session判断是否登陆?
服务器后端用j2ee架构的,前端Web端已有相应的登陆页面,登陆成功后用户信息时保存在Session中。在浏览器(PC浏览器或者手机浏览器)没有问题,但在APP(iOS)做完登陆POST请求后(请求request1),再做其它请求(请求request2),Session中并找不到刚才登陆的用户信息,貌似服务器对request1和request2是区别对待的,不认为是同一个client,该怎么操作呢?
iOS APP代码:
(1)APP打开,先执行登陆的POST请求(这步是成功的,User信息保存在服务器端Session中)
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let loginURL = "http://localhost:8080/m/login/loginCheck"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
userLogin()
}
//用户登陆
func userLogin() {
NetManager.POST(loginURL, parameters: ["userNo":"admin","pwd":"123"], success: { (responseObj) -> Void in
if let respDict = responseObj as? [String:NSObject] {
let ok = respDict["ok"] as! Bool
let errInfo = respDict["errInfo"] as! String
print("login: \(ok), errInfo: \(errInfo)")
}
}) { (error) in
print("login error: \(error)")
}
}
(2)在另外一个ViewController进行WebSocket请求
class ChatRoomPanelVC: UIViewController, WebSocketDelegate, UITableViewDataSource, UITableViewDelegate {
var socketURL:String = "ws://localhost:8080/msgWebSocket"
var socket:WebSocket!
//MARK: - properties -
var roomId:String!
override func viewDidLoad() {
super.viewDidLoad()
socket = WebSocket(url: NSURL(string: socketURL)!)
socket.delegate = self
socket.connect()
changeChatRoom(roomId)
}
//进入聊天室
func changeChatRoom(roomId:String) {
let requestStr:String = "change_room_id:" + roomId
print("requestStr:\(requestStr)")
socket.writeString(requestStr) // 这步请求发出后,第(3)步获取不到User信息
}
(3)服务器端
@Override
public void doFilter(ServletRequest req, ServletResponse rep,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) rep;
String uri = request.getRequestURI();
String fullUri = getFullUri(request);
if (StringUtilsEx.sameText(fullUri, request.getContextPath() + "/")
|| StringUtilsEx.sameText(fullUri, request.getContextPath())){
response.sendRedirect(request.getContextPath() + getUrlPermission().indexUrl(request, null));
return;
}
UserInfo loginUser = UserInfo.getLoginUser(request);
iOS端POST请求用的AFNetworking