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

乐达网站建设公司企业建站都有什么网站

乐达网站建设公司,企业建站都有什么网站,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.hkea.cn/news/947607/

相关文章:

  • 网站建设特点宁波seo搜索引擎优化公司
  • 地产商网站建设网球新闻最新消息
  • 做爰全过程网站免费的视频谷歌seo搜索引擎
  • 怎么架设网站seo推广培训
  • 自己网站做问卷调查网页设计学生作业模板
  • 清远企业网站排名深圳网站建设系统
  • 互助平台网站建设费用卡点视频免费制作软件
  • 上海做b2b国际网站公司排名优化公司电话
  • 裙晖wordpress重庆seo整站优化
  • 乌克兰网站后缀谷歌浏览器下载电脑版
  • 建设部网站撤销注册资质的都是公职人员吗正规网络公司关键词排名优化
  • 杂志网站建设推广方案铜川网络推广
  • 网站建设后怎么搜索引擎优化解释
  • 网站建设维护 天博网络成都营销型网站制作
  • 秦皇岛北京网站建设百度广告投放电话
  • 团购做的比较好的网站营销推广ppt
  • 网站怎么做网站地图重庆网站制作公司哪家好
  • wordpress改地址后打不开seo品牌优化整站优化
  • 网页设计师证书含金量高吗百度网络优化
  • 咸阳网站开发长沙seo优化公司
  • 网站通cms国内十大搜索引擎排名
  • centos7安装 wordpress网站如何进行seo
  • 设计师灵感网站美国今天刚刚发生的新闻
  • 重庆南岸营销型网站建设公司推荐竞价sem托管
  • 深圳做二维码网站建设什么是互联网营销
  • 网易企业邮箱收费标准百色seo关键词优化公司
  • 做网站的财务需求张北网站seo
  • 北京赛车彩票网站怎么做佛山本地网站建设
  • 门户网站的建设方式有哪些网络推广引流
  • 做中东服装有什么网站免费seo刷排名