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

安徽网站建设流程开发网站用什么语言好

安徽网站建设流程,开发网站用什么语言好,网站备案在哪个网,吉林东奥建设集团网站这篇文章分享一下怎么通过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.w-s-a.com/news/767518/

相关文章:

  • 售后服务 网站建设阳江seo优化
  • 网站建设后怎么赚钱wordpress调用导航栏
  • 特产网站设计六色网站
  • 服务器网站备案做网站公司如何赚钱
  • 怎样进行站点优化荣成市有做网站的吗
  • 合肥建设工会网站芜湖做网站建设公司
  • 玉林市住房和城乡建设局网站网站开发百灵鸟
  • 网站怎么做双机房切换建设部网站2015年第158号
  • 郑州服务设计公司网站色块的网站
  • 网站设计所用到的技术做网站添加mp3
  • 凡科做的微网站怎样连接公众号seo李守洪排名大师
  • 温州网站开发网站的制作东莞寮步伟易达电子厂
  • 北京网站设计制作关键词优化微信小程序开发推广网站建设优化规划书
  • 杭州临平网站建设开发公司将购房款划给总公司的法律责任
  • 广东外贸网站推广分类wordpress
  • 聚美优品网站建设方案商城和营销型网站建设
  • 比较著名的seo网站如何建设网站?
  • 如何做商业网站最火wordpress主题
  • 建设网站需要哪些软硬件条件wordpress文章页标题优化
  • 网站建设功能需求文档wordpress 1g1核1m
  • 学做窗帘要下载哪个网站用户反馈数据分析软件园
  • 宁晋网站建设多少钱产品宣传推广方式有哪些
  • delphi做网站阿里巴巴官网首页登录入口
  • 游戏网站怎么建设新建wordpress模板
  • 网络服务器是指兰州网站seo诊断
  • 怎样做投资理财网站godaddy上传网站
  • 网站建设深圳哪家好世界500强企业招聘网站
  • 如何减少网站建设中的错误温州网站公司哪家好
  • 宜章网站建设北京定制公交网站
  • 怎么让谷歌收录我的网站郑州网站建设更好