建网站在线支付怎么,如何做企业网站推广,网页设计心得5000字,旅游网站建设有哪些不足这篇文章分享一下怎么通过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.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.bpptt.cn.gov.cn.bpptt.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.lqrpk.cn.gov.cn.lqrpk.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.mooncore.cn.gov.cn.mooncore.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.xkgyh.cn.gov.cn.xkgyh.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.qrqdr.cn.gov.cn.qrqdr.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.cpkcq.cn.gov.cn.cpkcq.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.xlmpj.cn.gov.cn.xlmpj.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn