zoukankan      html  css  js  c++  java
  • Missing session attribute 'user' of type List 解决办法

     @GetMapping("/")
     public String index(@SessionAttribute(WebSecurityConfig.SESSION_KEY) List<Map<String, Object>>  list, Model model) {
    
                model.addAttribute("boy", list);
    
                return "index";
     }

    像上面这样,获取 session,如果session为空,无法返回模板,会提示

    Missing session attribute 'user' of type List   错误

    解决方法如下:

    用  HttpSession session  代替   @SessionAttribute(WebSecurityConfig.SESSION_KEY) List<Map<String, Object>> list

    这个是控制器里面的

     @GetMapping("/")
        public String index(HttpSession session,Model model) {
    
            Object obj =  session.getAttribute(WebSecurityConfig.SESSION_KEY);
            model.addAttribute("boy", obj);
            System.out.println(WebSecurityConfig.SESSION_KEY);
            return "index";
        }

    这样果session为空,也会返回模板,不会出错

    templates下的模板 index.html如下:

    <ul class="topNav">
    
                <li th:each="a:${boy}">
                    <span th:text="${a.user_id}"></span>
                    <span th:text="${a.user_name}"></span>
    
                </li>
    
    
            </ul>

    WebSecurityConfig”类如下

    package com.example.web;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    @Configuration
    public class WebSecurityConfig extends WebMvcConfigurerAdapter{
    
        /**
         * 登录session key
         */
        public final static String SESSION_KEY = "user";
    
        @Bean
        public SecurityInterceptor getSecurityInterceptor() {
            return new SecurityInterceptor();
        }
    
        public void addInterceptors(InterceptorRegistry registry) {
            InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
    
            // 排除配置
            //addInterceptor.excludePathPatterns("/error");
            addInterceptor.excludePathPatterns("/**");
    
            // 拦截配置
            addInterceptor.addPathPatterns("/admin/**");
        }
    
        private class SecurityInterceptor extends HandlerInterceptorAdapter {
    
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                HttpSession session = request.getSession();
                if (session.getAttribute(SESSION_KEY) != null)
                    return true;
    
                // 跳转登录
                String url = "/login";
                response.sendRedirect(url);
                return false;
            }
        }
    }

    登录那边的代码如下:

    // 用户登录
        public boolean login(String loginname, String password,HttpSession session) {
    
            sql = "select * from user where user_name = '"
                    + loginname + "' and password = '"
                    + password + "'";
            list = jdbcTemplate.queryForList(sql);
            if (!list.isEmpty()) {
    
                // 设置session
                session.setAttribute(WebSecurityConfig.SESSION_KEY, list );
    
    
                System.out.println(WebSecurityConfig.SESSION_KEY);
    
                flag = true;
            } else {
                flag = false;
            }
            return flag;
        }
  • 相关阅读:
    Nginx 基本命令
    Nginx配置详细
    MySQL 函数大全
    X-Frame-Options 配置
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    idea java 非web程序打包
    mysql 存储过程
    webstorm 重置所有设置
    vue input 赋值无效
    MySQL 性能优化神器 Explain 使用分析
  • 原文地址:https://www.cnblogs.com/kaka666/p/8343521.html
Copyright © 2011-2022 走看看