点开文字进入网站是怎么做的,教育直播网站开发,施工企业搭建的彩钢房如何做账务,中国网站建设公司目录
一、项目简介
二、设置静态资源访问路径
三、实现登录功能
四、拦截访问请求 本篇文章以黑马瑞吉外卖为例
一、项目简介
瑞吉外卖项目分为后台和前台系统#xff0c;后台提供给管理人员使用#xff0c;前台则是用户订餐使用
资源我们放在resources下 二、设置静态…目录
一、项目简介
二、设置静态资源访问路径
三、实现登录功能
四、拦截访问请求 本篇文章以黑马瑞吉外卖为例
一、项目简介
瑞吉外卖项目分为后台和前台系统后台提供给管理人员使用前台则是用户订餐使用
资源我们放在resources下 二、设置静态资源访问路径
我们的资源放在resources下当我们访问localhost:8080/backend/page/login/login.html后台登录时发现无法访问因为springboot只要静态资源放在类路径下 called /static (or /public or /resources or /META-INF/resources
才能访问所以我们需要自定义一下资源的访问路径
我们可以通过重写 WebMvcConfigurationSupport 的 addResourceHandlers 设置静态资源访问路径 代码如下
Configuration
Slf4j
public class WebMvcConfig extends WebMvcConfigurationSupport {/** 设置静态资源映射*/Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info(开始进行静态资源映射...);registry.addResourceHandler(/backend/**).addResourceLocations(classpath:/backend/);registry.addResourceHandler(/front/**).addResourceLocations(classpath:/front/);}
} 于是就能访问了 三、实现登录功能
通过网页检查-console 可以知道登录页面会向 employee/login 发送请求所以我们写一个controller实现对应的方法处理请求。对于登录请求的思路大致如下
因为数据库的密码使用md5加密所以需要先对密码进行md5加密将提交的用户名查询数据库是否有对应的用户无则提示登陆失败将查出的用户密码进行比对不同则则提示登陆失败将用户id放入session域中供后面使用
RestController
Slf4j
RequestMapping(/employee)
public class EmployeeController {Autowiredprivate EmployeeService employeeService;PostMapping(/login)public REmployee login(HttpServletRequest request, RequestBody Employee employee) {//1.将页面提交的密码password进行md5加密String password employee.getPassword();password DigestUtils.md5DigestAsHex(password.getBytes());//2.根据页面提交的用户名查询数据库LambdaQueryWrapperEmployee wrapper new LambdaQueryWrapper();wrapper.eq(Employee::getUsername, employee.getUsername());Employee user employeeService.getOne(wrapper);//3.找不到用户或者密码比对失败或者账户被禁用返回错误消息if(user null || !user.getPassword().equals(password)) {return R.error(登录失败);}if(user.getStatus() 0) {return R.error(账户已禁用);}//4.登陆成功将用户Id放入session域request.getSession().setAttribute(employee, user.getId());return R.success(user);}
} 登录成功 这里我们还要实现一下登出功能在右上角点击可以登录我们只需要写一个对应的方法即可需要清楚session域中的用户id返回退出成功的信息。 /** 员工退出*/PostMapping(/logout)public RString logout(HttpServletRequest request) {// 清除session中的用户Idrequest.getSession().removeAttribute(employee);return R.success(退出成功);}
四、拦截访问请求
登出后我们发现直接请求员工列表也是可行的这当然是不合理的如果没有登陆的请求应该返回登录页面我们可以通过一个全局过滤器实现请求的拦截步骤如下
对于不需要拦截的请求放行需要的我们查看session域中是否有用户id存在不存在则跳转回登陆页面
由于前端响应拦截器使用的是json形式所以我们需要返回 json 形式的对应字符串 WebFilter(filterName loginCheckFilter, urlPatterns /*)
Slf4j
public class LoginCheckFilter implements Filter {// 路径匹配器支持通配符public static final AntPathMatcher PATH_MATCHER new AntPathMatcher();Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request (HttpServletRequest) servletRequest;HttpServletResponse response (HttpServletResponse)servletResponse;// 1.获取请求的URIString requestURI request.getRequestURI();String[] urls new String[]{/employee/login,/employee/logout,/backend/**,/front/**};// 2.判断访问的URI需不需要登录才可访问log.info(拦截到请求{}, requestURI);if(check(urls, requestURI)) {log.info(本次请求{}不需要处理, requestURI);filterChain.doFilter(request, response);return;}// 3.已经登录无需处理直接放行if(request.getSession().getAttribute(employee) ! null) {log.info(用户已登录id为{} request.getSession().getAttribute(employee));filterChain.doFilter(request, response);return;}// 4. 如果未登录返回未登录结果通过输出流的方式向客户端返回响应数据log.info(用户未登录);response.getWriter().write(JSON.toJSONString(R.error(NOTLOGIN)));return;}/** 路径匹配判断是否需要放行*/public boolean check(String[] urls, String url) {for(String item : urls) {if(PATH_MATCHER.match(item, url)) {return true;}}return false;}
} 文章转载自: http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.xfcjs.cn.gov.cn.xfcjs.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.tlnbg.cn.gov.cn.tlnbg.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.lgmty.cn.gov.cn.lgmty.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.pfmsh.cn.gov.cn.pfmsh.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.trffl.cn.gov.cn.trffl.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn