fix the login bug

This commit is contained in:
fanfuxiaoran 2014-05-22 10:17:57 +08:00
parent 71b66be471
commit dc6db78f85
4 changed files with 24 additions and 180 deletions

View File

@ -1,162 +0,0 @@
package org.bench4q.web.api;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.models.master.AuthorizeResponseModel;
import org.bench4q.share.models.master.RegisterResponseModel;
import org.bench4q.share.models.master.UserModel;
import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
import org.bench4q.web.model.BaseResponseModel;
import org.bench4q.web.model.TestPlanTaskModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
public class AuthorizeActionController extends BaseControllerService {
private Logger logger = Logger.getLogger(AuthorizeActionController.class);
private String baseUrl = this.masterIp + "user";
private String INVALIDATE_INPUT = "invalidate input";
@RequestMapping("login")
public @ResponseBody
BaseResponseModel login(UserModel user, ModelMap model)
throws CustomGenericException {
isValidate(user);
AuthorizeResponseModel authorizeResponseModel = authorize(baseUrl
+ "/normalAuthorize", user);
return extractAuthorizeResponseModel(authorizeResponseModel, user,
model);
}
@RequestMapping("adminLogin")
public @ResponseBody
BaseResponseModel adminLogin(UserModel user, ModelMap model)
throws CustomGenericException {
isValidate(user);
AuthorizeResponseModel authorizeResponseModel = authorize(baseUrl
+ "/adminAuthorize", user);
return extractAuthorizeResponseModel(authorizeResponseModel, user,
model);
}
@RequestMapping("register")
@ResponseBody
public BaseResponseModel register(@RequestParam String userName,
@RequestParam String password, @RequestParam String scope)
throws CustomGenericException {
UserModel user = new UserModel();
user.setUserName(userName);
user.setPassword(password);
isValidate(user);
Map<String, String> params = this.makeParamsMap("userName",
user.getUserName());
params.put("password", user.getPassword());
params.put("scope", scope);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
baseUrl + "/register", params, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
throw new CustomGenericException("0",
"invalidate httpResponse",
"AuthorizeActionController_register");
}
RegisterResponseModel registerResponseModel = (RegisterResponseModel) ObjectXmlExchange
.fromXml(RegisterResponseModel.class,
httpResponse.getContent());
return extractRegisterModel(registerResponseModel);
} catch (IOException e) {
logger.info(e, e.fillInStackTrace());
throw new CustomGenericException("1",
"invalidate httpresponse:io exception", this.baseUrl
+ "/register");
} catch (JAXBException e) {
logger.info(e, e.fillInStackTrace());
throw new CustomGenericException("1",
"invalidate httpresponse:JAXBException", this.baseUrl
+ "/register");
}
}
private AuthorizeResponseModel authorize(String url, UserModel user)
throws CustomGenericException {
try {
Map<String, String> params = this.makeParamsMap("userName",
user.getUserName());
params.put("password", user.getPassword());
HttpResponse httpResponse = this.getHttpRequester().sendGet(url,
params, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
throw new CustomGenericException("1", "network error",
"AuthorizeActionController_authorize");
}
return (AuthorizeResponseModel) ObjectXmlExchange.fromXml(
AuthorizeResponseModel.class, httpResponse.getContent());
} catch (IOException e) {
logger.info(e, e.fillInStackTrace());
throw new CustomGenericException("1",
"invalidate httpResponse:IOException",
"AuthorizeActionController_authorize");
} catch (JAXBException e) {
logger.info(e, e.fillInStackTrace());
throw new CustomGenericException("1",
"invalidate httpResponse:JAXBException",
"AuthorizeActionController_authorize");
}
}
private BaseResponseModel extractAuthorizeResponseModel(
AuthorizeResponseModel authorizeResponseModel, UserModel user,
ModelMap model) {
if (!authorizeResponseModel.isSuccess()) {
logger.info("user name:" + user.getUserName());
logger.info("user password:" + user.getPassword());
logger.info("password or name wrong");
return new BaseResponseModel(false, "password or name wrong!");
} else {
model.addAttribute("accessToken",
authorizeResponseModel.getAccessToken());
model.addAttribute("username", user.getUserName());
List<TestPlanTaskModel> testPlanTaskModels = new ArrayList<TestPlanTaskModel>();
model.addAttribute("testPlanTaskList", testPlanTaskModels);
return new BaseResponseModel(true);
}
}
private BaseResponseModel extractRegisterModel(
RegisterResponseModel registerResponseModel) {
if (registerResponseModel.isSuccess())
return new BaseResponseModel(true);
else
return new BaseResponseModel(false, "");
}
private void isValidate(UserModel user) throws CustomGenericException {
if (user.getPassword() == null || user.getUserName() == null
|| user.getPassword().equals("")
|| user.getUserName().equals(""))
throw new CustomGenericException("0", INVALIDATE_INPUT, "");
}
}

View File

@ -1,4 +1,4 @@
package org.bench4q.web.newapi; package org.bench4q.web.api;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -10,16 +10,19 @@ import org.bench4q.share.models.master.RegisterResponseModel;
import org.bench4q.share.models.master.UserModel; import org.bench4q.share.models.master.UserModel;
import org.bench4q.web.masterMessager.UserMessager; import org.bench4q.web.masterMessager.UserMessager;
import org.bench4q.web.model.TestPlanTaskModel; import org.bench4q.web.model.TestPlanTaskModel;
import org.bench4q.web.newapi.BaseController;
import org.bench4q.web.validation.UserValidate; import org.bench4q.web.validation.UserValidate;
import org.bench4q.web.validation.ValidateResponseModel; import org.bench4q.web.validation.ValidateResponseModel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes({ "accessToken", "username", "testPlanTaskList" }) @SessionAttributes({ "accessToken", "username", "testPlanTaskList" })
public class UserController extends BaseController { public class UserController extends BaseController {
@ -50,7 +53,7 @@ public class UserController extends BaseController {
@RequestParam String password, @RequestParam String scope) { @RequestParam String password, @RequestParam String scope) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
ValidateResponseModel validateResponseModel = this.getUserValidate() ValidateResponseModel validateResponseModel = this.getUserValidate()
.validateRegisterUser(userName, password, scope); .validateRegisterUser(userName, password, scope);
if (!validateResponseModel.isSuccess()) { if (!validateResponseModel.isSuccess()) {
return fail(map, validateResponseModel.getMessage()); return fail(map, validateResponseModel.getMessage());
} }
@ -65,10 +68,9 @@ public class UserController extends BaseController {
} }
@RequestMapping("normalLogin") @RequestMapping("login")
@ResponseBody @ResponseBody
public Map<String, Object> normalLogin(UserModel userModel, public Map<String, Object> normalLogin(UserModel userModel, ModelMap modelMap) {
ModelMap modelMap) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
ValidateResponseModel validateResponseModel = this.getUserValidate() ValidateResponseModel validateResponseModel = this.getUserValidate()
.validateUserLogin(userModel.getUserName(), .validateUserLogin(userModel.getUserName(),
@ -78,6 +80,7 @@ public class UserController extends BaseController {
} }
AuthorizeResponseModel authorizeResponseModel = this.getUserMessager() AuthorizeResponseModel authorizeResponseModel = this.getUserMessager()
.normalLogin(userModel.getUserName(), userModel.getPassword()); .normalLogin(userModel.getUserName(), userModel.getPassword());
System.out.println(authorizeResponseModel.isSuccess());
if (authorizeResponseModel.isSuccess()) { if (authorizeResponseModel.isSuccess()) {
updateSessionAttribute(modelMap, authorizeResponseModel, updateSessionAttribute(modelMap, authorizeResponseModel,
userModel.getUserName()); userModel.getUserName());

View File

@ -11,7 +11,6 @@ import org.bench4q.web.masterMessager.AgentManagerMessager;
import org.bench4q.web.validation.AgentValidate; import org.bench4q.web.validation.AgentValidate;
import org.bench4q.web.validation.ValidateResponseModel; import org.bench4q.web.validation.ValidateResponseModel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;

View File

@ -1,5 +1,4 @@
function checkName(name) { function checkName(name) {
if (name == '') { if (name == '') {
@ -24,22 +23,21 @@ function checkPass(password) {
} }
} }
$("body").keydown(function(e) { $("body").keydown(function(e) {
var ee = e || window.event; var ee = e || window.event;
if(ee.keyCode == 13){ if (ee.keyCode == 13) {
login(); login();
} }
}); });
function login() { function login() {
var userName = $('#username').val(); var userName = $('#username').val();
var password = $('#password').val(); var password = $('#password').val();
$.post('normalLogin', { $.post('login', {
userName : userName, userName : userName,
password : password password : password
}, function(data) { }, function(data) {
if (data.success) { if (data.success) {
$('#loginMsg').hide(); $('#loginMsg').hide();
window.location.replace("homepage.jsp"); window.location.replace("homepage.jsp");
@ -49,7 +47,7 @@ function login() {
else { else {
alert(data.failedMessage); alert(data.failedMessage);
} }
},"json"); }, "json");
} }
function adminLogin() { function adminLogin() {
@ -83,7 +81,7 @@ function register() {
$.post('register', { $.post('register', {
userName : userName, userName : userName,
password : password, password : password,
rePassword:rePassword rePassword : rePassword
}, function(data) { }, function(data) {
if (data.success) { if (data.success) {
$('#registerMsg').hide(); $('#registerMsg').hide();
@ -99,6 +97,12 @@ function register() {
}, "json"); }, "json");
} }
$("#img").mouseover(function(){document.getElementById("img").setAttribute("src", "img/githubsignon1.jpg");}); $("#img").mouseover(
function() {
document.getElementById("img").setAttribute("src",
"img/githubsignon1.jpg");
});
$("#img").mouseout(function(){document.getElementById("img").setAttribute("src", "img/githubsignon.jpg");}); $("#img").mouseout(function() {
document.getElementById("img").setAttribute("src", "img/githubsignon.jpg");
});