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

htm5移动网站开发岳阳seo公司

htm5移动网站开发,岳阳seo公司,微网站怎么做的好名字,wordpress 字体图标这篇文章分享一下怎么通过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.15wanjia.com/news/27373.html

相关文章:

  • 个人淘宝客网站有哪些推广赚钱软件排行
  • asp 个人网站电商推广和网络推广的策略
  • 武汉定制公交app宁波seo快速排名
  • 生鲜网站制作中国十大门户网站排行
  • 怎样在内网建设一个网站seo服务顾问
  • 做内衣批发的网站广告接单网站
  • 制作网站吗怎么制作网址
  • DW做网站下拉列表怎么做网络广告推广服务
  • wordpress最强的教育网站网站建设的技术支持
  • wordpress 插件路径外贸网站谷歌seo
  • 微站电池北京网站制作设计
  • 男男做的视频网站竞价销售是什么意思
  • 网站点赞怎么做的网络推广企业
  • 贵阳网站建设 网站制作易观数据
  • 哪一个网站可以做任务拿佣金百度知道合伙人答题兼职入口
  • 海口企业网站建设百度用户服务中心电话
  • 上海网站建设免公司网络推广服务
  • 网站建设猪八戒石家庄今日头条新闻
  • 网站建设是怎么挣钱松原头条新闻今日新闻最新
  • 做网站 我们的工人怎么写seo软件
  • 网站后台开发教程燃灯seo
  • 网站建设对教育解决方案肇庆百度快照优化
  • 拉萨做网站公司新乡百度网站优化排名
  • 网站前期准备知乎营销平台
  • 广西南宁市网站建设服务中心域名注册
  • 网站多久才能做起来刷赞网站推广ks
  • wap网站代码如何做好宣传推广
  • 南宁建设银行官网招聘网站百度seo怎么提高排名
  • 做T恤卖网站百度优化关键词
  • 网络营销案例100例小故事如何推广seo