This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/controller/LoginController.java
2018-12-12 12:00:46 +08:00

253 lines
8.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.nis.util.Configurations;
import com.nis.util.Constants;
import com.nis.util.CookieUtil;
import com.nis.util.StringUtil;
import com.nis.util.StringUtils;
import com.nis.util.httpclient.HttpClientUtil;
import com.nis.web.security.IdGen;
import com.nis.web.security.SystemAuthorizingRealm.Principal;
import com.nis.web.security.UserUtils;
import com.nis.web.service.SystemService;
@Controller
public class LoginController extends BaseController {
@Autowired
private SystemService systemService;
@RequestMapping("${adminPath}/newIndex")
public String newIndex(){
return "/newIndex";
}
/**
* 根据ip46,协议tcp,udp查询带宽 间隔5分钟数据
*/
@RequestMapping(value="${adminPath}/newIndex/ntcEntranceReport")
@ResponseBody
public List ntcEntranceReport(){
Map<String, Object> fromJsonList = new HashMap<String, Object>();
List list = new ArrayList();
/*String url = "http://192.168.10.204:7777/galaxy-service/service/log/v1/ntcEntranceReport?searchBusinessType=2&searchReportStartTime=2018-07-18%2000:00:00&searchReportEndTime=2018-07-19%2000:00:00";
try {
String string = HttpClientUtil.get(url);
Gson gson = new GsonBuilder().create();
fromJsonList = gson.fromJson(string, new TypeToken<Map>(){}.getType());
logger.info("首页,间隔5分钟数据"+fromJsonList);
Map map = (Map) fromJsonList.get("data");
list = (List) map.get("list");
} catch (Exception e) {
e.printStackTrace();
logger.error("带宽详情数据获取错误"+e);
}*/
return list;
}
/**
* 默认进入系统动作,即跳转登录操作
* @return
*/
@RequestMapping(value="login",method=RequestMethod.GET)
public String login(HttpServletRequest request, HttpServletResponse response, Model model){
Principal principal = UserUtils.getPrincipal();
logger.debug("login, active session size: {}"+systemService.getActiveSessions(false).size());
if (Constants.TRUE.equals(Configurations.getStringProperty("notAllowRefreshIndex", "false"))) {
try {
CookieUtil.addCookie(response, "LOGINED", "false");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 如果已经登录,则跳转到管理首页
if(principal != null && !principal.isMobileLogin()){
return "redirect:" + adminPath + "/index";
}
return "login";
}
/**
* 登录失败时调用的方法真正登录方法在shiro filter时实现。
* @return
*/
@RequestMapping(value="login",method=RequestMethod.POST)
public String loginFail(HttpServletRequest request, HttpServletResponse response, Model model){
Principal principal = UserUtils.getPrincipal();
// 如果已经登录,则跳转到管理首页
if(principal != null){
return "redirect:" + adminPath + "/index";
}
String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
boolean rememberMe = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM);
boolean mobile = WebUtils.isTrue(request, Constants.DEFAULT_MOBILE_PARAM);
String exception = (String)request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
String message = (String)request.getAttribute(Constants.DEFAULT_MESSAGE_PARAM);
if (StringUtils.isBlank(message) || StringUtils.equals(message, "null")){
message = "用户或密码错误, 请重试!";
}
model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, username);
model.addAttribute(FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM, rememberMe);
model.addAttribute(Constants.DEFAULT_MOBILE_PARAM, mobile);
model.addAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME, exception);
model.addAttribute(Constants.DEFAULT_MESSAGE_PARAM, message);
if (logger.isDebugEnabled()){
logger.debug("login fail, active session size: "+systemService.getActiveSessions(false).size()+", message: "+message+", exception: "+exception);
}
// 非授权异常登录失败验证码加1。
if (!UnauthorizedException.class.getName().equals(exception)){
model.addAttribute("isValidateCodeLogin", UserUtils.isValidateCodeLogin(username, true, false));
}
// 验证失败清空验证码
request.getSession().setAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, IdGen.uuid());
// 如果是手机登录则返回JSON字符串
if (mobile){
return renderString(response, model);
}
return "login";
}
@RequiresPermissions("user")
@RequestMapping("/nis/index")
public String index(HttpServletRequest request, HttpServletResponse response,ModelMap model){
Principal principal = UserUtils.getPrincipal();
// 登录成功后,验证码计算器清零
UserUtils.isValidateCodeLogin(principal.getLoginName(), false, true);
if (logger.isDebugEnabled()){
logger.debug("show index, active session size: {}"+ systemService.getActiveSessions(false).size());
}
// 如果已登录,再次访问主页,则退出原账号。
if (Constants.TRUE.equals(Configurations.getStringProperty("notAllowRefreshIndex","false"))){
String logined;
try {
logined = CookieUtil.getValue(request, "LOGINED");
if (StringUtils.isBlank(logined) || "false".equals(logined)){
CookieUtil.addCookie(response, "LOGINED", "true");
}else if (StringUtils.equals(logined, "true")){
UserUtils.getSubject().logout();
return "redirect:" + "/login";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 如果是手机登录则返回JSON字符串
if (principal.isMobileLogin()){
if (request.getParameter("login") != null){
return renderString(response, principal);
}
if (request.getParameter("index") != null){
return "home";
}
return "redirect:" + "/login";
}
model.addAttribute("adminPath", adminPath);
model.addAttribute("log_time_start",Constants.LOG_TIME_START);
model.addAttribute("log_time_range",Constants.LOG_TIME_RANGE);
model.addAttribute("log_total",this.getMsgProp().getProperty("log_total"));
return "/home";
}
/**
* 获取主题方案
*/
@RequestMapping(value = "/theme/{theme}")
public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request, HttpServletResponse response){
try {
if (StringUtils.isNotBlank(theme)){
CookieUtil.addCookie(response, "theme", theme);
}else{
theme = CookieUtil.getValue(request, "theme");
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:"+request.getParameter("url");
}
/**
* 修改权限组
*/
@RequestMapping(value = "/permission/{roleId}")
public String switchPermissionGroup(@PathVariable String roleId, HttpServletRequest request, HttpServletResponse response){
try {
if (StringUtils.isNotBlank(roleId)){
System.out.println("*****************");
}
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:"+request.getParameter("url");
}
@RequestMapping(value="/validateCode")
public void validateCode(HttpServletRequest request, HttpServletResponse response,String captcha) {
renderString(response, UserUtils.validateCodeIsValid(captcha));
}
}