当前位置: 首页 > news >正文

广州做网站优化费用福建省高速公路建设管理网站

广州做网站优化费用,福建省高速公路建设管理网站,正规制作网站公司哪家好,襄阳机械加工网这篇文章分享一下怎么通过session结合过滤器来实现控制登录访问的功能#xff0c;涉及的代码非常简单#xff0c;通过session保存用户登录的信息#xff0c;如果没有用户登录的话#xff0c;会在过滤器中处理#xff0c;重定向回登录页面。 创建一个springboot项目#…这篇文章分享一下怎么通过session结合过滤器来实现控制登录访问的功能涉及的代码非常简单通过session保存用户登录的信息如果没有用户登录的话会在过滤器中处理重定向回登录页面。 创建一个springboot项目添加springbooot-starter-web和lombok的依赖。创建对应的实体类、controller、service并创建两个简单的html页面测试过滤器的效果。 一、登录功能实现 controller package cn.edu.sgu.www.login.controller;import cn.edu.sgu.www.login.entity.User; import cn.edu.sgu.www.login.service.UserService; import cn.edu.sgu.www.login.util.UserUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import java.io.IOException;/*** author heyunlin* version 1.0*/ RestController RequestMapping(path /user, produces application/json;charsetutf-8) public class UserController {private final UserService userService;Autowiredpublic UserController(UserService userService) {this.userService userService;}RequestMapping(value /login, method RequestMethod.POST)public void login(User user) throws IOException {userService.login(user);UserUtils.getResponse().sendRedirect(/index.html);}} service UserService package cn.edu.sgu.www.login.service;import cn.edu.sgu.www.login.entity.User;/*** author heyunlin* version 1.0*/ public interface UserService {/*** 登录认证* param user 用户输入的信息*/void login(User user); } UserServiceImpl package cn.edu.sgu.www.login.service.impl;import cn.edu.sgu.www.login.entity.User; import cn.edu.sgu.www.login.service.UserService; import cn.edu.sgu.www.login.util.UserUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;/*** author heyunlin* version 1.0*/ Service public class UserServiceImpl implements UserService {private final UserUtils userUtils;Autowiredpublic UserServiceImpl(UserUtils userUtils) {this.userUtils userUtils;}Overridepublic void login(User user) {String username user.getUsername();String password user.getPassword();if (username null || .equals(username)) {throw new RuntimeException(用户名不能为空~);} else if (password null || .equals(password)) {throw new RuntimeException(密码不能为空~);} else {if (username.equals(admin) password.equals(12345)) {userUtils.getSession().setAttribute(user, user);} else {throw new RuntimeException(用户名或密码错误);}}}} 二、过滤器实现资源访问控制 LoginFilter package cn.edu.sgu.www.login.filter;import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException;/*** 登录过滤器* author heyunlin* version 1.0*/ WebFilter(filterName loginFilter, urlPatterns {/, /html/*, /index.html}) public class LoginFilter implements Filter {Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request (HttpServletRequest) req;HttpSession session request.getSession();// 获取登录信息Object obj session.getAttribute(user);if (obj null) { // 未登录重定向到登录页/** 登录页面的地址*/String loginPage /login.html;// 获取响应对象HttpServletResponse response (HttpServletResponse) resp;response.sendRedirect(loginPage);} else { // 当前有用户登录放行filterChain.doFilter(req, resp);}}} 在任意配置类上使用ServletComponentScan(cn.edu.sgu.www.login.filter)开启servlet的组件扫描~ package cn.edu.sgu.www.login;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;ServletComponentScan(cn.edu.sgu.www.login.filter) SpringBootApplication public class FilterLoginApplication {public static void main(String[] args) {SpringApplication.run(FilterLoginApplication.class, args);}} 文章设计的代码已上传到git仓库可按需获取~ 使用过滤器实现一个最简单的登录认证功能https://gitee.com/he-yunlin/filter-login.git
http://www.w-s-a.com/news/624482/

相关文章:

  • 成都全美网站建设江苏专业网站建设
  • 足球网站模板有帮忙做阿里巴巴网站的吗
  • 建设厅报名网站京东网站的建设与发展前景
  • 金寨县住房和城乡建设部网站网页作业怎么做一个网站
  • 做ppt模板网站有哪些内容wap是什么意思卡老师
  • 网站建设一定要域名吗网站后台关键词设置
  • 标书制作公司网站坪山网站建设哪家便宜
  • 防止做网站的人修改数值门户网站架构
  • 电子项目外包网站考二建需要什么学历和专业
  • 做网站推广引流效果好吗电商推广技巧
  • 亦庄网站建设价格广州网站推广服务
  • 十大免费ppt网站下载重庆在线高校平台登录
  • 做环保网站案例百度seo教程
  • 体育用品网站模板网站建设话术
  • 潍坊网站建设服务商做网站多久能盈利
  • 嘉定区做网站房产信息查询官网
  • 网站直播间 是怎么做的唐山论坛建站模板
  • 深圳洲聚网站建设wordpress 泛解析
  • 五金东莞网站建设技术支持wordpress 添加模板
  • 网站申请专利春节网页设计素材
  • 进网站备案md风格的wordpress主题
  • 如何建站网站十大免费建站app
  • 工作号做文案素材的网站晋城网站设计人
  • 建设部网站官网 施工许可怎样建网站 需要
  • 什么网站都能打开的浏览器同城小程序怎么推广
  • 在电脑上怎么做网站网址seo分析
  • 石家庄做网站网络公司电子商务营销推广
  • 网站开发 前端专做婚礼logo的网站
  • 同创企业网站建设拖拽建设网站源码
  • wordpress调用网站标题网站页面排版