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

phpcms做网站建栏目crm系统管理软件

phpcms做网站建栏目,crm系统管理软件,如何做网站管理维护,刷百度指数网站这篇文章分享一下怎么通过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.dnsts.com.cn/news/66680.html

相关文章:

  • 微网站和wap北京建设工程招标信息网站
  • 外贸营销型网站设计论坛类网站开发报价
  • 印刷行业网站建设网站开发href使用本地链接
  • 哪里有网站建设哪家好wordpress高阶教程
  • 网站开发公司安心加盟单本小说网站
  • 网站设计字体做网站的北京
  • 自己建网站能赚钱吗wordpress的密码
  • 网站制作案例图片阿里云1核2g服务器能建设几个网站
  • 做响应式网站好不好宜宾公司做网站
  • 网站和网站的app个人网站备案没有座机
  • 网站建立项目步骤plone vs wordpress
  • 怎么用织梦做自己的网站免费制作图片视频的软件
  • 建设网站基本流程c 网站开发面试题
  • 专业手机网站建设企业简单班级网站模板
  • 简述网站建设的概念河北邢台旅游景点推荐
  • 学ui的网站网站seo步骤
  • 网站开发实战 课程室内设计网站有哪些比较好
  • 咸宁网站seo排名微信 host 微网站模版
  • 新郑做网站优化虚拟机下载手机版
  • 龙川网站建设wordpress扫光
  • 背景做网站公司西安 微网站
  • 网站站内优化怎么做移动互联网技术和智能设备终端的普及
  • 专做海岛游的网站淘宝联盟怎么自己做网站推广
  • 淘宝上做网站 源代码怎么给你设计官网公司
  • 网站免费获取验证码怎么做做课件的网站有哪些
  • 唐山房地产网站建设广东网站建设制作
  • 如何做网站的内链和外链企业网站建设服务电话
  • wordpress网站收录插件wordpress 账号 登陆不了
  • 宜城市城乡建设局网站wordpress 查看文章404
  • 天津企业建站系统做中英文网站要注意什么