filter logincontroller

@RequestMapping("login")
public  String  login(String  account,HttpServletRequest  request){
System.out.println("我进来了");
  if(account !=""){
if(account.contains("zhupengwei")){
request.getSession().setAttribute("admin", account);
}else{

request.getSession().setAttribute("username", account);
}
return  "redirect:/menu.jsp";
  }else{
  return  "redirect:/index.jsp";
  }
  


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Spring Security来实现这个功能,在Spring Security中可以配置一个过滤器,当用户未登录时自动跳转到登录页面。 首先,需要添加Spring Security的依赖到项目中: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 然后,在启动类上添加@EnableWebSecurity注解,并实现WebSecurityConfigurer接口: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**"); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 在上面的代码中,configure方法配置了访问控制和登录页面,configureGlobal方法配置了一个内存中的用户,用于测试。 最后,在Controller中添加/login路由的处理方法,用于展示登录页面: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } } ``` 在上面的代码中,返回的字符串"login"对应的是一个名为"login.html"的Thymeleaf模板文件。 现在,当用户未登录时,访问任何需要授权的路由,都会被重定向到/login路由,展示登录页面。登录成功后,用户会被重定向回原先要访问的页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值