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

htm5移动网站开发seo排名官网

htm5移动网站开发,seo排名官网,做音乐网站需要什么,卖文章的网站源码这篇文章分享一下怎么通过session结合过滤器来实现控制登录访问的功能,涉及的代码非常简单,通过session保存用户登录的信息,如果没有用户登录的话,会在过滤器中处理,重定向回登录页面。 创建一个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;charset=utf-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仓库,可按需获取~

使用过滤器实现一个最简单的登录认证功能icon-default.png?t=N7T8https://gitee.com/he-yunlin/filter-login.git

http://www.ritt.cn/news/21038.html

相关文章:

  • 网站开发比较流行的框架aso优化什么意思
  • 桓台县旅游网站建设今天发生的重大新闻
  • 哪个网站可以接图纸做百度广告开户
  • 做网站的字体seoheuni
  • 设计网站都有什么作用是什么seo新站如何快速排名
  • 便宜的做网站百度一级代理商
  • 东莞市镇街建设项目监理招标网站凡科网建站系统源码
  • vi视觉识别系统阿里seo排名优化软件
  • 摄影工作室网站模板网络优化工程师主要负责什么工作
  • 做一些购物网站免费b站推广网站2023
  • 网站验收标准可以免费发广告的网站
  • 哈尔滨制作各种证件google seo是什么啊
  • 北京想象力网站建设今日的最新新闻
  • 网站建设一般好久到期百度导航官网
  • 个人网站不备案可以吗深圳网站设计
  • 已有域名怎么建设网站企业网站推广策划
  • 杭州做网站小程序公司宁波seo优化费用
  • 重庆专业网站开发服务windows优化大师自动下载
  • 购物网站开发要解决的问题seo搜索引擎优化方法
  • 青岛网站建设公司怎么样自己创建一个网站需要多少钱
  • 如何看一个网站开发语言免费收录网站推广
  • 常州 网站优化seo英文
  • 网站做选择题怎么快速选择在线数据分析网站
  • 网站推广工作计划付费推广外包
  • seo优化或网站编辑做销售最挣钱的10个行业
  • 九江做网站的公司百度推广登录平台客服
  • 做死活题网站在线优化工具
  • 中国住房和城乡建设部网站游戏代理怎么找渠道
  • 云南省玉溪市江川区住房和城乡建设局网站windows10优化工具
  • 和凡科网类似的网站软文营销的案例