springboot项目配置延长session失效时间

session大家都知道失效时间大概也就在30分左右,在springboot项目中配置一下也简单,我就直接上代码了=====

在springboot启动类 application.java中加入此配置就可以了:

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》===

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,MybatisAutoConfiguration.class})
@ComponentScan("com.tianrun")
@RestController
public class Application 

{

//启动类main方法

    public static void main( String[] args )
    {
        SpringApplication.run(Application.class, args);
    }
    //加入以下配置就行
    @Bean 
    public EmbeddedServletContainerCustomizer containerCustomizer(){ 
    return new EmbeddedServletContainerCustomizer() { 
    @Override 
    public void customize(ConfigurableEmbeddedServletContainer container) { 
    container.setSessionTimeout(1800);//单位为S 
    }
    }; 
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot项目中,可以使用Session来实现数据的共享。Session是一种在服务器端存储用户数据的机制,可以在不同的请求之间传递数据。 要在Spring Boot项目中使用Session共享数据,可以按照以下步骤进行操作: 1. 配置Session管理器:在Spring Boot的配置文件(如application.properties或application.yml)中,可以配置Session的管理器。例如,可以设置Session的超时时间、Cookie的属性等。 2. 创建Controller:创建一个Controller类,用于处理请求和响应。在Controller中,可以使用`@SessionAttributes`注解来指定需要共享的数据模型。 3. 存储数据到Session:在Controller中,可以使用`HttpSession`对象来存储和获取数据。可以使用`setAttribute`方法将数据存储到Session中,使用`getAttribute`方法从Session中获取数据。 4. 使用共享数据:在其他Controller或页面中,可以通过`HttpSession`对象来获取共享的数据。可以使用`getAttribute`方法获取存储在Session中的数据。 下面是一个示例代码,演示了如何在Spring Boot项目中使用Session共享数据: ```java @Controller @SessionAttributes("username") public class UserController { @GetMapping("/login") public String login(Model model) { model.addAttribute("username", "John"); return "login"; } @GetMapping("/dashboard") public String dashboard(Model model) { return "dashboard"; } } ``` 在上面的示例中,`@SessionAttributes("username")`注解指定了需要共享的数据模型,即"username"。在`login`方法中,将"John"存储到Session中。在`dashboard`方法中,可以通过`HttpSession`对象获取存储在Session中的"username"数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值