一套简单的登录、鉴权工具( 三 )

数据库目前用的是mysql,使用时要记得添加驱动依赖
SpringBoot整合代码PS:我们自定义DbUtil工具类获取连接操作,SpringBoot项目需要带上时区、字符集参数
jdbc:mysql://localhost/jfinal_demo?serverTimezone=GMT%2B8&characterEncoding=utf-8新建一个springboot项目或在我们的springBoot项目中随便挑一个来测试
首先需要将springboot-exceptionhandler项目中自定义统一异常处理相关代码拷贝过来,方便捕获我们抛出的业务异常
然后新建一个AccessAuthorityFilter拦截器
/** * SpringBoot测试鉴权拦截器 */@WebFilter(filterName = "AccessAuthorityFilter",urlPatterns = {"/**"})@ServletComponentScan@Componentpublic class AccessAuthorityFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {//请求头HttpServletRequest request = (HttpServletRequest) servletRequest;SecurityUtil securityUtil = SecurityUtil.getInstance();//鉴权中心String auc = securityUtil.auc(request);if("UNAUTHORIZED".equals(auc)){throw new ServiceException(ErrorEnum.UNAUTHORIZED);}if("FORBIDDEN".equals(auc)){throw new ServiceException(ErrorEnum.FORBIDDEN);}//执行filterChain.doFilter(servletRequest, servletResponse);}@Overridepublic void destroy() {}}写几个测试接口,包括login登录、logout注销等
/** * 测试接口 */@RestControllerpublic class TestController {/*** 简单登录、注销、获取登录用户*/@GetMapping("/login")public String login(HttpServletRequest request,String username, String password){SecurityUtil securityUtil = SecurityUtil.getInstance();SecurityUtil.User user = securityUtil.getUserByUserNameAndPassword(username, password);if(user != null){securityUtil.setLoginUser(request,user);return "登录成功!";}else{return "账号或密码错误...";}}@GetMapping("/logout")public String logout(HttpServletRequest request){SecurityUtil securityUtil = SecurityUtil.getInstance();SecurityUtil.User loginUser = securityUtil.getLoginUser(request);securityUtil.setLoginUser(request,null);return "注销成功!";}@GetMapping("/getLoginUser")public HashMap<String, Object> getLoginUser(HttpServletRequest request){SecurityUtil securityUtil = SecurityUtil.getInstance();SecurityUtil.User loginUser = securityUtil.getLoginUser(request);List<SecurityUtil.Role> loginUserRole = securityUtil.getLoginUserRole(request);HashMap<String, Object> map = new HashMap<>(2);map.put("loginUser",loginUser);map.put("loginUserRole",loginUserRole);return map;}/*** 登录、鉴权测试接口*/@GetMapping("/sys/xtgl")public String xtgl() {return "系统管理...";}@GetMapping("/sys/yhgl")public String yhgl() {return "用户管理...";}@GetMapping("/portal/mhgl")public String mhgl() {return "网站门户管理...";}@GetMapping("/portal/index")public String portalIndex() {return "网站门户首页...";}}

一套简单的登录、鉴权工具

文章插图
 
效果未登录时,只有配置在无需登录即可访问的URL才能允许访问
一套简单的登录、鉴权工具

文章插图
 
登录后,除了无需权限的URL,还可以访问角色允许访问的URL,注销后恢复登录前状态
一套简单的登录、鉴权工具

文章插图
 
SpringBoot项目比较常规大家用的也比较多,代码就不上传了
JFinal整合代码创建一个访问权限拦截器AccessAuthorityInterceptor
package cn.huanzi.qch.interceptor;import cn.huanzi.qch.common.model.ErrorEnum;import cn.huanzi.qch.common.model.ServiceException;import cn.huanzi.qch.util.SecurityUtil;import com.jfinal.aop.Interceptor;import com.jfinal.aop.Invocation;import com.jfinal.log.Log;import javax.servlet.http.HttpServletRequest;/** * 访问权限拦截器 */public class AccessAuthorityInterceptor implements Interceptor {private static final Log log = Log.getLog(AccessAuthorityInterceptor.class);@Overridepublic void intercept(Invocation invocation) {//请求头HttpServletRequest request = invocation.getController().getRequest();SecurityUtil securityUtil = SecurityUtil.getInstance();//鉴权中心String auc = securityUtil.auc(request);if("UNAUTHORIZED".equals(auc)){throw new ServiceException(ErrorEnum.UNAUTHORIZED);}if("FORBIDDEN".equals(auc)){throw new ServiceException(ErrorEnum.FORBIDDEN);}invocation.invoke();}}AppConfig中注册拦截器
/** * API 引导式配置 */public class AppConfig extends JFinalConfig {//省略其他代码.../*** 配置路由*/public void configRoute(Routes me) {//省略其他代码...// 此处配置 Routes 级别的拦截器,可配置多个me.addInterceptor(new AccessAuthorityInterceptor());}//省略其他代码...}


推荐阅读