【问题标题】:Invalidate session (logout) in Spring Boot MVC appSpring Boot MVC 应用程序中的无效会话(注销)
【发布时间】:2019-08-01 13:22:17
【问题描述】:

我正在尝试以最简单的方式登录和注销Spring MVC。我是 .NET 人,我记得我很快就在 ASP.NET 中实现了会话身份验证。 现在,我必须使用Spring MVC,而我面临的问题是我在logout 方法中得到了不同的会话对象,所以我不能取消它。 我在login 方法中创建会话属性,我使用我的逻辑的地方在我的ProductsController 内。这是代码:

登录方式:

 @PostMapping
    @RequestMapping(value = {"login"},method = RequestMethod.POST)
    public ResponseEntity Login(@RequestBody @Valid LoginModel user, HttpServletRequest request,HttpSession session){



        List<User> userFromDb=service.findByEmailAndPassword(user.getEmail(),user.getPassword());
        if(!userFromDb.isEmpty()){
            session.setAttribute("loggedInUser", user );
            return new ResponseEntity(HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }

注销方法:

 @GetMapping
   @RequestMapping(value = {"logout"},method = RequestMethod.GET)
   public ResponseEntity Logout(HttpServletRequest request, SessionStatus 
 status,HttpSession session, HttpServletResponse response){

    session.invalidate();
//        request.getSession(true).removeAttribute("loggedInUser");
//
//        request.getSession(true).invalidate();
    Cookie[] cookies = request.getCookies();
    if(cookies!=null) {
        for (Cookie cookie : cookies) {
            cookie.setMaxAge(0);
            cookie.setValue(null);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
    }
    return new ResponseEntity(HttpStatus.OK);
}

产品控制器:

RequestMapping(value = {"","/"},method = RequestMethod.GET)
    @GetMapping
    public ResponseEntity Products(HttpServletRequest request, HttpSession session){

        if(session==null || session.getAttribute("loggedInUser")==null){
            return new ResponseEntity(HttpStatus.UNAUTHORIZED);
        }
        List<Product> products= service.getAll();
        return new ResponseEntity(products,HttpStatus.OK);
    }

我可以登录并且用户保存在会话中。但是,当我注销时,我仍然可以访问我的产品页面,这意味着该会话没有被清除。

如果这很重要,我正在使用 create-react-app 端口 localhost:3000 上的前端创建 react 应用程序,而我的服务器在 localhost:8080 上。

【问题讨论】:

  • 最简单的方法是不编写任何代码并使用内置所有这些的 Spring Security。
  • @Chrylis 你能给我一些好的教程或博客文章吗?有很多不同的方式,很多配置,很多类名,bean 之类的概念。考虑到这是我第一次编写 Spring 代码,这绝对不简单

标签: java spring spring-boot spring-mvc session


【解决方案1】:

WebSecurityConfigurerAdapter 配置类(实现)中使用 spring 安全约束和注销配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    .
    ...
    ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
         http
          .csrf().disable()
          .authorizeRequests()
          .antMatchers("/login*").permitAll()
          .anyRequest().authenticated()  
          .anyRequest().authenticated()
          ....
          .
          .
          .and()
          .logout()
          .invalidateHttpSession(true)
          .deleteCookies("JSESSIONID")

    }
    ...
    ...
    ...    
}

【讨论】:

  • 这段代码是什么意思,我在哪里保存用户到会话?放在哪里?是的,我以前看过这段代码。我知道使用复制/粘贴,但我进入 Spring 的时间不够长,没有足够的知识
  • 在项目中搜索您的 WebSecurityConfigurerAdapter 实现?否则你不使用弹簧安全?还有@VladoPandžić 你正在使用哪个版本的弹簧?
  • 我会在 PC 上看到我的版本,我几天前创建的项目可能是最新的。这是关于路由的配置类,但我如何在登录方法中验证用户。类似 SpringSecurity.autheticateUserUsingSession(user)
  • 可能您使用的是手动版本,而不是配置版本?也是在xml还是注解中
  • Spring Boot 注释版本
猜你喜欢
  • 1970-01-01
  • 2012-01-21
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
相关资源
最近更新 更多