hejq 7 лет назад
Родитель
Сommit
7f9dd750af

+ 52 - 19
src/main/java/com/uas/platform/b2bManage/controller/AccountController.java

@@ -8,6 +8,8 @@ import com.uas.platform.b2bManage.model.User;
 import com.uas.platform.b2bManage.page.exception.IllegalOperatorException;
 import com.uas.platform.b2bManage.service.UseLogService;
 import com.uas.platform.b2bManage.service.UserService;
+import com.uas.platform.b2bManage.support.MyException;
+import com.uas.platform.b2bManage.support.SecurityConstant;
 import com.uas.platform.b2bManage.web.BaseController;
 import com.uas.platform.core.util.AgentUtils;
 import com.uas.platform.core.util.encry.Md5Utils;
@@ -20,6 +22,7 @@ import org.springframework.web.bind.support.SessionStatus;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 
@@ -48,35 +51,59 @@ public class AccountController extends BaseController {
 	    return success(SystemSession.getUser());
 	}
 
-	/**
-	 * 登录
-	 *
-	 */
+    /**
+     * 登录
+     *
+     * @param userName 用户名 手机或邮箱
+     * @param passWord 密码 明文
+     * @return ModelMap
+     *  <pre>
+     *      success : 是否成功
+     *      content : 用户信息
+     *      url : 访问路径
+     *  </pre>
+     * @throws IllegalAccessException
+     * @throws UnsupportedEncodingException
+     */
 	@RequestMapping(value = "/login", method = RequestMethod.POST)
-	public void login(String userName, String passWord) throws IllegalAccessException, UnsupportedEncodingException {
+	public ModelMap login(String userName, String passWord) throws IllegalAccessException, UnsupportedEncodingException {
         userService.login(userName.trim(), passWord.trim(), request, response);
         useLogService.appendLog(UseType.LOGIN.code(), null, AgentUtils.getIp(request));
+        ModelMap map = new ModelMap();
+        User user = SystemSession.getUser();
+        String returnUrl = null != user ? SecurityConstant.INDEX_URL : SecurityConstant.LOGIN_URL;
+        boolean logSuccess = null != user ? true : false;
+        map.put("success", logSuccess);
+        map.put("content", user);
+        map.put("url", returnUrl);
+        return map;
 	}
 
-	/**
-	 * 退出
-	 *
-	 * @throws IOException
-	 */
+    /**
+     * 退出
+     *
+     * @param sessionStatus session状态
+     * @param session session
+     * @return 访问路径
+     * @throws IOException IO异常
+     */
 	@RequestMapping(value = "/logout", method = RequestMethod.POST)
-	public ModelMap logout(SessionStatus sessionStatus) throws IOException {
+	public ModelMap logout(SessionStatus sessionStatus, HttpSession session) throws IOException {
 		SystemSession.clear();
-        request.getSession().invalidate();
+        session.invalidate();
         sessionStatus.setComplete();
-        useLogService.appendLog(UseType.LOGOUT.code(), null, AgentUtils.getIp(request));
-		return success();
+        User user = SystemSession.getUser();
+        if (null != user) {
+            logout(sessionStatus, session);
+        }
+		return new ModelMap("url", SecurityConstant.LOGIN_URL);
 	}
 
     /**
      * 检验手机号
      *
      * @param tel 手机号码
-     * @return
+     * @return ModelMap success  true
      */
 	@RequestMapping(value = "/valid/tel", method = RequestMethod.POST)
     public ModelMap validTel(String tel) {
@@ -91,7 +118,7 @@ public class AccountController extends BaseController {
      * 检验邮箱
      *
      * @param email 邮箱
-     * @return
+     * @return ModelMap success  true
      */
     @RequestMapping(value = "/valid/email", method = RequestMethod.POST)
     public ModelMap validEmail(String email) {
@@ -107,7 +134,11 @@ public class AccountController extends BaseController {
      * 注册
      *
      * @param user 用户信息
-     * @return
+     * @return ModelMap
+     * <pre>
+     *     success: true
+     *     content: user
+     * </pre>
      */
     @RequestMapping(value = "/register", method = RequestMethod.POST)
     public ModelMap register(User user) {
@@ -122,7 +153,9 @@ public class AccountController extends BaseController {
         if (StringUtils.isEmpty(email)) {
             throw new IllegalOperatorException("请输入邮箱地址");
         }
-        if (email.contains("=")) {
+        // 等于符号
+        String equalSymbol = "=";
+        if (email.contains(equalSymbol)) {
             email = email.replace("=", "");
         }
         email = email + Constant.EMAIL_SUFFIX;
@@ -133,7 +166,7 @@ public class AccountController extends BaseController {
      * 通过链接修改密码
      */
     @RequestMapping(value = "/resetPwd/url", method = RequestMethod.GET)
-    public void resetPwdByUrl(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, NotFoundException, ServletException {
+    public void resetPwdByUrl(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, MyException, ServletException {
         userService.resetPwd(secretKey, response, request);
     }
 

+ 21 - 9
src/main/java/com/uas/platform/b2bManage/service/UserService.java

@@ -1,6 +1,7 @@
 package com.uas.platform.b2bManage.service;
 
 import com.uas.platform.b2bManage.model.User;
+import com.uas.platform.b2bManage.support.MyException;
 import javassist.NotFoundException;
 
 import javax.servlet.ServletException;
@@ -12,7 +13,8 @@ import java.io.UnsupportedEncodingException;
 /**
  * 用户接口
  *
- * Created by hejq on 2018-04-23.
+ * @author hejq
+ * @date 2018-04-23
  */
 public interface UserService {
 
@@ -20,7 +22,7 @@ public interface UserService {
      * 通过电话号码查询用户信息
      *
      * @param tel 电话
-     * @return
+     * @return User
      */
     User findByTel(String tel);
 
@@ -28,15 +30,15 @@ public interface UserService {
      * 通过邮箱查询用户信息
      *
      * @param email 邮箱
-     * @return
+     * @return User
      */
     User findUserByUserEmail(String email);
 
     /**
      * 通过用户名查询用户信息
      *
-     * @param name
-     * @return
+     * @param name 用户名
+     * @return User
      */
     User findUserByName(String name);
 
@@ -45,14 +47,18 @@ public interface UserService {
      *
      * @param userName 账号
      * @param passWord 密码
+     * @param request request
+     * @param response response
+     * @throws MyException 自定义异常
+     * @throws UnsupportedEncodingException 编码异常
      */
-    void login(String userName, String passWord, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, UnsupportedEncodingException;
+    void login(String userName, String passWord, HttpServletRequest request, HttpServletResponse response) throws MyException, UnsupportedEncodingException;
 
     /**
      * 注册用户
      *
      * @param user 用户信息
-     * @return
+     * @return User
      */
     User register(User user);
 
@@ -68,15 +74,21 @@ public interface UserService {
      *
      * @param secretKey 秘钥
      * @param response 请求
-     * @return
+     * @param request request
+     * @return User
+     * @throws IOException IO异常
+     * @throws MyException 自定义异常
+     * @throws ServletException 服务器异常
      */
-    User resetPwd(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, NotFoundException, ServletException;
+    User resetPwd(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, MyException, ServletException;
 
     /**
      * 修改密码
      *
      * @param id 用户id
      * @param password 密码
+     * @param response  response
+     * @throws IOException IO异常
      */
     void resetPassword(Long id, String password, HttpServletResponse response) throws IOException;
 }

+ 23 - 18
src/main/java/com/uas/platform/b2bManage/service/impl/UserServiceImpl.java

@@ -1,6 +1,5 @@
 package com.uas.platform.b2bManage.service.impl;
 
-import com.alibaba.fastjson.JSON;
 import com.uas.message.mail.service.MailService;
 import com.uas.platform.b2bManage.core.support.SystemSession;
 import com.uas.platform.b2bManage.core.util.StringUtils;
@@ -10,11 +9,10 @@ import com.uas.platform.b2bManage.model.Constant;
 import com.uas.platform.b2bManage.model.SecretKeyRecord;
 import com.uas.platform.b2bManage.model.User;
 import com.uas.platform.b2bManage.service.UserService;
+import com.uas.platform.b2bManage.support.MyException;
 import com.uas.platform.b2bManage.support.SecurityConstant;
 import com.uas.platform.b2bManage.support.StringUtil;
-import com.uas.platform.core.util.AgentUtils;
 import com.uas.platform.core.util.encry.Md5Utils;
-import javassist.NotFoundException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.stereotype.Service;
@@ -26,14 +24,13 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
 import java.util.List;
 
 /**
  * 用户数据接口
  *
- * Created by hejq on 2018-04-23.
+ * @author hejq
+ * @date 2018-04-23
  */
 @Service
 public class UserServiceImpl implements UserService {
@@ -51,7 +48,7 @@ public class UserServiceImpl implements UserService {
      * 通过电话号码查询用户信息
      *
      * @param tel 电话
-     * @return
+     * @return User
      */
     @Override
     public User findByTel(String tel) {
@@ -63,7 +60,7 @@ public class UserServiceImpl implements UserService {
      * 通过邮箱查询用户信息
      *
      * @param email 邮箱
-     * @return
+     * @return User
      */
     @Override
     public User findUserByUserEmail(String email) {
@@ -75,7 +72,7 @@ public class UserServiceImpl implements UserService {
      * 通过用户名查询用户信息
      *
      * @param name
-     * @return
+     * @return User
      */
     @Override
     public User findUserByName(String name) {
@@ -91,23 +88,26 @@ public class UserServiceImpl implements UserService {
      *
      * @param userName 账号
      * @param passWord 密码
+     * @param request request
+     * @param response response
+     * @throws MyException 自定义异常
+     * @throws UnsupportedEncodingException 编码异常
      */
     @Override
-    public void login(String userName, String passWord, HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, UnsupportedEncodingException {
+    public void login(String userName, String passWord, HttpServletRequest request, HttpServletResponse response) throws MyException, UnsupportedEncodingException {
         List<User> users = userDao.findByTel(userName);
         if (CollectionUtils.isEmpty(users)) {
             users = userDao.findByEmail(userName);
             if (CollectionUtils.isEmpty(users)) {
-                throw new IllegalAccessException("未找到账号信息");
+                throw new MyException("未找到账号信息");
             }
         }
         User user = users.get(0);
         if (Md5Utils.encode(passWord, user.getName()).equals(user.getPassword())) {
             SystemSession.setUser(user);
             request.getSession().setAttribute("user", user);
-            request.getSession().setAttribute("ipAddress", AgentUtils.getIp(request));
         } else {
-            throw new IllegalAccessException("账号或密码错误");
+            throw new MyException("账号或密码错误");
         }
     }
 
@@ -115,7 +115,7 @@ public class UserServiceImpl implements UserService {
      * 注册用户
      *
      * @param user 用户信息
-     * @return
+     * @return User
      */
     @Override
     public User register(User user) {
@@ -156,11 +156,15 @@ public class UserServiceImpl implements UserService {
      * 通过访问链接修改密码
      *
      * @param secretKey 秘钥
-     * @param response  请求
-     * @return
+     * @param response 请求
+     * @param request request
+     * @return User
+     * @throws IOException
+     * @throws MyException
+     * @throws ServletException
      */
     @Override
-    public User resetPwd(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, NotFoundException, ServletException {
+    public User resetPwd(String secretKey, HttpServletResponse response, HttpServletRequest request) throws IOException, MyException, ServletException {
         if (StringUtils.isEmpty(secretKey)) {
             response.sendRedirect(Constant.INVALIDURL);
         }
@@ -174,7 +178,7 @@ public class UserServiceImpl implements UserService {
             } else {
                 List<User> users = userDao.findByEmail(record.getEmail().trim());
                 if (CollectionUtils.isEmpty(users)) {
-                    throw new NotFoundException("未找到该邮箱用户信息");
+                    throw new MyException("未找到该邮箱用户信息");
                 } else {
                     SystemSession.setUser(users.get(0));
                     request.getSession().setAttribute("user", users.get(0));
@@ -191,6 +195,7 @@ public class UserServiceImpl implements UserService {
      *
      * @param id 用户id
      * @param password 密码
+     * @throws IOException
      */
     @Override
     public void resetPassword(Long id, String password, HttpServletResponse response) throws IOException {

+ 9 - 1
src/main/java/com/uas/platform/b2bManage/support/SecurityConstant.java

@@ -1,7 +1,10 @@
 package com.uas.platform.b2bManage.support;
 
 /**
- * Created by hejq on 2018-04-23.
+ * 系统路径
+ *
+ * @author hejq
+ * @date 2018-04-23
  */
 public class SecurityConstant {
 
@@ -14,4 +17,9 @@ public class SecurityConstant {
      * 认证信息
      */
     public static final String AUTHENTICATION_URL = "/authentication";
+
+    /**
+     * 主页
+     */
+    public static final String INDEX_URL = "/index";
 }

+ 9 - 4
src/main/java/com/uas/platform/b2bManage/web/filter/SSOInterceptor.java

@@ -43,7 +43,7 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
         User user = SystemSession.getUser();
         // 未登录则要求登录
         if (user == null) {
-            logoutSession();
+            logoutSession(request);
             if(!this.onAuthenticateFailed(request, response)) {
                 return false;
             } else {
@@ -62,8 +62,7 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
      */
     private void logSession(HttpServletRequest request) throws UnsupportedEncodingException {
         Object user = request.getSession().getAttribute("user");
-        Object ip = request.getSession().getAttribute("ipAddress");
-        if (user != null && AgentUtils.getIp(request).equals(ip)) {
+        if (user != null) {
             SystemSession.setUser((User) user);
             log.info("登录成功," + JSON.toJSONString(user));
         }
@@ -72,10 +71,16 @@ public class SSOInterceptor extends HandlerInterceptorAdapter implements Filter
     /**
      * 线程池策略下,不会频繁删除线程,置于线程内的对象须手动删除
      */
-    private void logoutSession() {
+    private void logoutSession(HttpServletRequest request) {
         SystemSession.clear();
+        request.getSession().invalidate();
     }
 
+    /**
+     * 验证成功,获取用户信息
+     *
+     * @param request
+     */
     protected void onAuthenticateSuccess(HttpServletRequest request) {
         User user = (User) request.getAttribute("user");
         if (user != null) {

+ 9 - 2
src/main/webapp/resources/js/account/signIn.js

@@ -20,8 +20,15 @@ function login() {
         data: user,
         method: 'POST',
         async: false,
-        success: function() {
-            window.location.href = "/enterprise";
+        success: function(data) {
+            if (data.success) {
+                window.loginInfo = data;
+                $('.x-nologin').hide();
+                $('.x-login').show();
+
+                $('.x-login').find('.title').text(data.content.name);
+            }
+            window.location.href = data.url;
         },
         error: function (error) {
             alert(error.responseText);

+ 14 - 6
src/main/webapp/resources/js/common/common.js

@@ -27,18 +27,26 @@ function getAccountInfo() {
  */
 function Logout() {
     $.ajaxSetup({cache : false });
+    clearAllCookie();
+    var config = {
+        cache: false,
+        headers: {
+            'Cache-Control': 'no-cache',
+            'Pragma': 'no-cache',
+        },
+        ifModified :true ,
+    };
     $.ajax('logout', {
         method: 'POST',
         async: false,
-        success: function() {
-            clearAllCookie();
-            window.location.href = "/signIn";
+        config: config,
+        success: function(data) {
+            window.location.href = data.url || '/index';
         },
-        error: function (error) {
-            toastr.error(error);
+        error: function (res) {
+            toastr.error(res.responseText);
         }
     });
-    window.location.href = "/signIn";
 }
 
 //清除所有cookie函数