新手学习spring boot第三课
程序员文章站
2024-03-14 10:05:28
...
spring boot第三课----简单的登陆界面
简单的输入密码登录
这里就没有连接数据库了,下面的修改login.html,中要修改form表单来进行登录请求:
然后再写一个LoginController请求
package com.atguigu.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
@RequestMapping
public class LoginController {
//@GetMapping
//@PutMapping
//@DeleteMapping
//@PatchMapping
//或者@RequestMapping(value="/user/login",method = RequestMethod.POST )
@PostMapping(value="/user/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Map<String,Object> map,
HttpSession session)
{
if (!StringUtils.isEmpty(username)&&"123456".equals(password))
{
//就算登陆成功,就来到主页,防止表单重复提交,考研重定向到主页
session.setAttribute("loginUser",username);
//return "dashboard";
return "redirect:/main.html";
}
else
{
//登陆失败
map.put("msg","用户名密码错误");
return "login";
}
}
}
run一下,如果密码错误(不是123456)就会:
密码正确:
然后重定向
没有重定向,刷新会这样的:
可见还是上次那个页面,防止重新提交开头重定向一下:
第一步:
记住这里没有“:”,我因为这个就浪费了好几个小时了
代码:
package com.atguigu.springboot.config;
import com.atguigu.springboot.component.LoginHandlerInterceptor;
import com.atguigu.springboot.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//使用WebMvcConfigurerAdapter可以开展springMVC的功能要什么方法就重写功能,Ctrl+O
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
//浏览器发送/atguigu请求来到success
registry.addViewController("/atguigu").setViewName("success");
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册到容器中
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer configurer = new WebMvcConfigurer(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
// //所有的WebMvcConfigurer组件都会一起起作用
// @Bean //将组件注册在容器中
// public WebMvcConfigurer webMvcConfigurer(){
// WebMvcConfigurer configurer = new WebMvcConfigurer(){
// @Override
// public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/").setViewName("login");
// registry.addViewController("/index.html").setViewName("login");
// registry.addViewController("/main.html").setViewName("dashboard");
// }
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// super.addInterceptors(registry);
//静态资源; *.css , *.js
//SpringBoot已经做好了静态资源映射
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/webjars/**","/asserts/**");
}
};
return configurer;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
代码和上面那个一样的,就不发了,然后就run一下,成功登进去后结果:
最后就是拦截器了
首先创建一个LoginHandlerInterceptor来做登录检查,
代码:
package com.atguigu.springboot.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*登陆检查,没有登陆的用户就不能增删改查
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginUser");
if(user == null){
//未登陆,返回登陆页面
request.setAttribute("msg","没有权限请先登陆");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else{
//已登陆,放行请求
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
登陆进去:
可以修改dashboard.html来显示usename
然后ctrl+F9重新编写一下就OK了,上面我那个多一个}
应该为