Commit d40aac75 by fengshuonan

更新整个登录过程 抽出来不需要鉴权的请求路径的常量

parent bf194930
...@@ -43,6 +43,8 @@ import java.util.HashMap; ...@@ -43,6 +43,8 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import static cn.stylefeng.guns.core.common.constant.Const.NONE_PERMISSION_RES;
/** /**
* shiro权限管理的配置 * shiro权限管理的配置
* *
...@@ -175,11 +177,9 @@ public class ShiroConfig { ...@@ -175,11 +177,9 @@ public class ShiroConfig {
* *
*/ */
Map<String, String> hashMap = new LinkedHashMap<>(); Map<String, String> hashMap = new LinkedHashMap<>();
hashMap.put("/assets/**", "anon"); for (String nonePermissionRe : NONE_PERMISSION_RES) {
hashMap.put("/gunsApi/**", "anon"); hashMap.put(nonePermissionRe, "anon");
hashMap.put("/login", "anon"); }
hashMap.put("/global/sessionError", "anon");
hashMap.put("/kaptcha", "anon");
hashMap.put("/**", "user"); hashMap.put("/**", "user");
shiroFilter.setFilterChainDefinitionMap(hashMap); shiroFilter.setFilterChainDefinitionMap(hashMap);
return shiroFilter; return shiroFilter;
......
...@@ -17,6 +17,7 @@ package cn.stylefeng.guns.config.web; ...@@ -17,6 +17,7 @@ package cn.stylefeng.guns.config.web;
import cn.stylefeng.guns.config.properties.GunsProperties; import cn.stylefeng.guns.config.properties.GunsProperties;
import cn.stylefeng.guns.core.common.controller.GunsErrorView; import cn.stylefeng.guns.core.common.controller.GunsErrorView;
import cn.stylefeng.guns.core.interceptor.AttributeSetInteceptor;
import cn.stylefeng.guns.core.interceptor.RestApiInteceptor; import cn.stylefeng.guns.core.interceptor.RestApiInteceptor;
import cn.stylefeng.guns.core.listener.ConfigListener; import cn.stylefeng.guns.core.listener.ConfigListener;
import cn.stylefeng.roses.core.xss.XssFilter; import cn.stylefeng.roses.core.xss.XssFilter;
...@@ -44,6 +45,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; ...@@ -44,6 +45,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
import static cn.stylefeng.guns.core.common.constant.Const.NONE_PERMISSION_RES;
/** /**
* web 配置类 * web 配置类
* *
...@@ -73,6 +76,7 @@ public class WebConfig implements WebMvcConfigurer { ...@@ -73,6 +76,7 @@ public class WebConfig implements WebMvcConfigurer {
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RestApiInteceptor()).addPathPatterns("/gunsApi/**"); registry.addInterceptor(new RestApiInteceptor()).addPathPatterns("/gunsApi/**");
registry.addInterceptor(new AttributeSetInteceptor()).excludePathPatterns(NONE_PERMISSION_RES).addPathPatterns("/**");
} }
/** /**
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
*/ */
package cn.stylefeng.guns.core.common.constant; package cn.stylefeng.guns.core.common.constant;
import cn.hutool.core.collection.CollectionUtil;
import java.util.List;
/** /**
* 系统常量 * 系统常量
* *
...@@ -48,4 +52,9 @@ public interface Const { ...@@ -48,4 +52,9 @@ public interface Const {
*/ */
String API_MENU_NAME = "接口文档"; String API_MENU_NAME = "接口文档";
/**
* 不需要权限验证的资源表达式
*/
List<String> NONE_PERMISSION_RES = CollectionUtil.newLinkedList("/assets/**", "/gunsApi/**", "/login", "/global/sessionError", "/kaptcha", "/error", "/global/error");
} }
/**
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.stylefeng.guns.core.interceptor;
import cn.stylefeng.guns.core.listener.ConfigListener;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自动渲染当前用户信息登录属性 的过滤器
*
* @author fengshuonan
* @Date 2018/10/30 4:30 PM
*/
public class AttributeSetInteceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//没有视图的直接跳过过滤器
if (modelAndView == null || modelAndView.getViewName() == null) {
return;
}
//视图结尾不是html的直接跳过
if (!modelAndView.getViewName().endsWith("html")) {
return;
}
ShiroUser user = ShiroKit.getUser();
if (user == null) {
throw new AuthenticationException("当前没有登录账号!");
} else {
modelAndView.addObject("menus", user.getMenus());
modelAndView.addObject("name", user.getName());
if (ToolUtil.isEmpty(user.getAvatar())) {
modelAndView.addObject("avatar", ConfigListener.getConf().get("contextPath") + "/assets/images/users/1.jpg");
} else {
modelAndView.addObject("avatar", user.getAvatar());
}
modelAndView.addObject("email", user.getEmail());
}
}
}
...@@ -15,11 +15,6 @@ ...@@ -15,11 +15,6 @@
*/ */
package cn.stylefeng.guns.modular.system.controller; package cn.stylefeng.guns.modular.system.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.core.listener.ConfigListener;
import cn.stylefeng.guns.core.util.ApiMenuFilter;
import cn.stylefeng.guns.modular.system.service.IMenuService;
import cn.stylefeng.guns.modular.system.service.INoticeService; import cn.stylefeng.guns.modular.system.service.INoticeService;
import cn.stylefeng.roses.core.base.controller.BaseController; import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -37,15 +32,12 @@ import java.util.Map; ...@@ -37,15 +32,12 @@ import java.util.Map;
* @Date 2017年3月4日23:05:54 * @Date 2017年3月4日23:05:54
*/ */
@Controller @Controller
@RequestMapping("/blackboard") @RequestMapping("/dashboard")
public class DashboardController extends BaseController { public class DashboardController extends BaseController {
@Autowired @Autowired
private INoticeService noticeService; private INoticeService noticeService;
@Autowired
private IMenuService menuService;
/** /**
* 跳转到黑板 * 跳转到黑板
*/ */
...@@ -53,19 +45,6 @@ public class DashboardController extends BaseController { ...@@ -53,19 +45,6 @@ public class DashboardController extends BaseController {
public String blackboard(Model model) { public String blackboard(Model model) {
List<Map<String, Object>> notices = noticeService.list(null); List<Map<String, Object>> notices = noticeService.list(null);
model.addAttribute("noticeList", notices); model.addAttribute("noticeList", notices);
//获取菜单列表
List<MenuNode> tempMenus = menuService.getMenusByRoleIds(CollectionUtil.newArrayList(1));
List<MenuNode> menus = MenuNode.buildTitle(tempMenus);
menus = ApiMenuFilter.build(menus);
model.addAttribute("menus", menus);
//获取用户头像
model.addAttribute("name", "stylefeng");
model.addAttribute("avatar", ConfigListener.getConf().get("contextPath") + "/assets/images/users/1.jpg");
model.addAttribute("email", "sn93@qq.com");
return "/dashboard.html"; return "/dashboard.html";
} }
} }
...@@ -16,16 +16,12 @@ ...@@ -16,16 +16,12 @@
package cn.stylefeng.guns.modular.system.controller; package cn.stylefeng.guns.modular.system.controller;
import cn.stylefeng.guns.core.common.exception.InvalidKaptchaException; import cn.stylefeng.guns.core.common.exception.InvalidKaptchaException;
import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.core.log.LogManager; import cn.stylefeng.guns.core.log.LogManager;
import cn.stylefeng.guns.core.log.factory.LogTaskFactory; import cn.stylefeng.guns.core.log.factory.LogTaskFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit; import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser; import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.ApiMenuFilter;
import cn.stylefeng.guns.core.util.KaptchaUtil; import cn.stylefeng.guns.core.util.KaptchaUtil;
import cn.stylefeng.guns.modular.system.model.User; import cn.stylefeng.guns.modular.system.service.INoticeService;
import cn.stylefeng.guns.modular.system.service.IMenuService;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.base.controller.BaseController; import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil; import cn.stylefeng.roses.core.util.ToolUtil;
import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Constants;
...@@ -38,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -38,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List; import java.util.List;
import java.util.Map;
import static cn.stylefeng.roses.core.util.HttpContext.getIp; import static cn.stylefeng.roses.core.util.HttpContext.getIp;
...@@ -51,34 +48,24 @@ import static cn.stylefeng.roses.core.util.HttpContext.getIp; ...@@ -51,34 +48,24 @@ import static cn.stylefeng.roses.core.util.HttpContext.getIp;
public class LoginController extends BaseController { public class LoginController extends BaseController {
@Autowired @Autowired
private IMenuService menuService; private INoticeService noticeService;
@Autowired
private IUserService userService;
/** /**
* 跳转到主页 * 跳转到主页
*/ */
@RequestMapping(value = "/", method = RequestMethod.GET) @RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) { public String index(Model model) {
//获取菜单列表
List<Integer> roleList = ShiroKit.getUser().getRoleList(); List<Integer> roleList = ShiroKit.getUser().getRoleList();
if (roleList == null || roleList.size() == 0) { if (roleList == null || roleList.size() == 0) {
ShiroKit.getSubject().logout(); ShiroKit.getSubject().logout();
model.addAttribute("tips", "该用户没有角色,无法登陆"); model.addAttribute("tips", "该用户没有角色,无法登陆");
return "/login.html"; return "/login.html";
} }
List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
List<MenuNode> titles = MenuNode.buildTitle(menus);
titles = ApiMenuFilter.build(titles);
model.addAttribute("titles", titles);
//获取用户头像 //主页包含通知列表
Integer id = ShiroKit.getUser().getId(); List<Map<String, Object>> notices = noticeService.list(null);
User user = userService.selectById(id); model.addAttribute("noticeList", notices);
String avatar = user.getAvatar();
model.addAttribute("avatar", avatar);
return "/index.html"; return "/index.html";
} }
......
...@@ -37,6 +37,6 @@ ...@@ -37,6 +37,6 @@
<div class="sidebar-footer"> <div class="sidebar-footer">
<a href="" class="link" data-toggle="tooltip" title="编辑资料"><i class="mdi mdi-account"></i></a> <a href="" class="link" data-toggle="tooltip" title="编辑资料"><i class="mdi mdi-account"></i></a>
<a href="" class="link" data-toggle="tooltip" title="修改密码"><i class="ti-settings"></i></a> <a href="" class="link" data-toggle="tooltip" title="修改密码"><i class="ti-settings"></i></a>
<a href="" class="link" data-toggle="tooltip" title="退出登录"><i class="mdi mdi-power"></i></a> <a href="${ctxPath}/logout" class="link" data-toggle="tooltip" title="退出登录"><i class="mdi mdi-power"></i></a>
</div> </div>
</aside> </aside>
@layout("/common/_container.html",{title:"概览"}){
<div class="row">
<div class="col-12">
@for(notice in noticeList){
<div class="alert alert-info" role="alert">
${notice.content}
</div>
@}
</div>
</div>
@}
...@@ -24,20 +24,20 @@ ...@@ -24,20 +24,20 @@
<div class="login-register" style="background-image:url(../assets/images/login-register.jpg);"> <div class="login-register" style="background-image:url(../assets/images/login-register.jpg);">
<div class="login-box card"> <div class="login-box card">
<div class="card-block"> <div class="card-block">
<form class="form-horizontal form-material" id="loginform" action="index.html"> <form class="form-horizontal form-material" id="loginform" action="${ctxPath}/login" method="post">
<h3 class="box-title m-b-30">Guns管理系统</h3> <h3 class="box-title m-b-30">Guns管理系统</h3>
<div class="form-group "> <div class="form-group ">
<div class="col-xs-12"> <div class="col-xs-12">
<input class="form-control" type="text" required="" placeholder="账号" autocomplete="off"></div> <input class="form-control" name="username" type="text" required="" placeholder="账号" autocomplete="off"></div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="col-xs-12"> <div class="col-xs-12">
<input class="form-control" type="password" required="" placeholder="密码" autocomplete="off"></div> <input class="form-control" name="password" type="password" required="" placeholder="密码" autocomplete="off"></div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="col-md-12 p-l-0"> <div class="col-md-12 p-l-0">
<div class="checkbox checkbox-primary p-t-0"> <div class="checkbox checkbox-primary p-t-0">
<input id="checkbox-signup" type="checkbox"> <input id="checkbox-signup" type="checkbox" name="remember">
<label for="checkbox-signup"> 记住我 </label> <label for="checkbox-signup"> 记住我 </label>
</div> </div>
</div> </div>
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
</div> </div>
<div class="form-group m-b-0"> <div class="form-group m-b-0">
<div class="col-sm-12 text-center"> <div class="col-sm-12 text-center">
<p>没有账号? <a href="register.html" class="text-info m-l-5"><b>点击注册</b></a></p> <p>没有账号? <a href="#" class="text-info m-l-5"><b>点击注册</b></a></p>
</div> </div>
</div> </div>
</form> </form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment