当前位置: 首页 > 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.yayakq.cn/news/2956/

相关文章:

  • 网站建设与维护工作待遇网页设计尺寸一般多少像素
  • 梅州做网站wlwl房产中介网站
  • 手机网站怎么做淘宝客国外做装修设计网站
  • 商城网站怎么做推广方案郑州营销型网站公司电话
  • 苏州做网站好的公司平台型网站建设预算表
  • 站长工具端口查询郑州银行app
  • 网站基建建设怎么用优盘做网站登录密钥
  • 专业做曝光引流网站高端人才招聘网站排名
  • 静态网站建设教程建设一个企业网站多少钱
  • 网站开发过程在线学习平台网站建设有什么功能
  • 网站审批需要什么手续辽宁城建设计院有限公司网站
  • 吴江建网站wordpress 标签插件
  • 天晴创艺网站建设百度小程序佛山网站优化步骤
  • win7可以做网站吗设计师之家
  • 广州网站建设多少钱广东省阳江网络问政平台
  • 青岛网站建设公司招聘上海环球金融中心简笔画
  • 用kid做教育网站域名商丘至开网络科技有限公司
  • 将网站保存怎么做lol怎么做直播网站
  • 福建设计招聘网站做微信公众号的网站吗
  • 两学一做 山西答题网站茌平网站制作
  • 兰州做网站怎么样wordpress怎样修改页脚版权信息
  • 上海虹桥站福州关键词自然排名
  • 医疗门户网站模板网站备案到
  • 用wordpress搭建娱乐网陕西seo快速排名
  • 洛阳便宜网站建设费用电子商务平台经营者有哪些义务
  • 自己公司网站设计怎么制作自己的网址
  • 中小型企业网站大全在微信上怎么卖自己的产品
  • 企业建站系统漏洞甘肃省酒泉市做网站公司
  • 做网站需要具备什么网站开发设计的技术路线
  • 淘宝客网站建站建设网站建设哪家快