SpringBoot web开发(2)session登录注册

SpringBoot web开发(2)session登录注册

  1. 新建 LoginController.java 文件
  2. 使用 session 控制登录
@Autowired
HttpSession session;
//添加sessionKey
session.setAttribute("name", "admin");
//获取session
session.getAttribute("name")
//删除
session.removeAttribute("name");

示例:(简单控制登录)

  • 主页控制器
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HomeController{

    @Autowired
    HttpSession session;

    @RequestMapping("/")
    public String Home(){
        if(!"admin".equals(session.getAttribute("name"))){
            //重定向 到 登录页面
            return "redirect:/login/";
        }
        //登录成功
        return "home/index";
    }
}

  • 登录控制器
package com.learn.first.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/login")
public class LoginController{

    @Autowired
    HttpSession session;

    @RequestMapping("")
    public String index(){
        return "login/index";
    }

    @RequestMapping("/in")
    public RedirectView in(@RequestParam String lname,@RequestParam String pwd){
        if("admin".equals(lname) && "admin".equals(pwd)){
            RedirectView redirectTarget = new RedirectView();
            redirectTarget.setContextRelative(true);
            redirectTarget.setUrl("/");
            session.setAttribute("name", "admin");
            
            return redirectTarget;
        }

        RedirectView redirectTarget = new RedirectView();
        redirectTarget.setContextRelative(true);
        redirectTarget.setUrl("/login/");
        return redirectTarget;
    }

    @RequestMapping("/out")
    public String out(){
        // session.setAttribute("name", null);
        session.removeAttribute("name");
        return "redirect:/login/";
    }
}

pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.7.RELEASE
		 
	
	com.learn
	first
	0.0.1-SNAPSHOT
	first
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			nz.net.ultraq.thymeleaf
			thymeleaf-layout-dialect
		
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



  1. 使用 thymeleaf模板 返回页面
  2. 打开浏览器,http://127.0.0.1:8080 测试
  3. demo

你可能感兴趣的:(java,spring,java,Spring,Boot)