package com.itheima.controller; import java.util.Enumeration; import javax.servlet.http.HttpSession; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class FilmeController { // 影片详情页 @GetMapping("/detail/{type}/{path}") public String toDetail(@PathVariable("type") String type, @PathVariable("path") String path) { return "detail/" + type + "/" + path; } // 向用户登录页面跳转 @GetMapping("/userLogin") public String toLoginPage() { return "login/login"; } /** * 通过传统的HttpSession获取Security控制的登录用户信息 * * @param session */ @GetMapping("/getuserBySession") @ResponseBody public void getUser(HttpSession session) { // 从当前HttpSession获取绑定到此会话的所有对象的名称 Enumeration<String> names = session.getAttributeNames(); while (names.hasMoreElements()) { // 获取HttpSession中会话名称 String element = names.nextElement(); // 获取HttpSession中的应用上下文 SecurityContextImpl attribute = (SecurityContextImpl) session.getAttribute(element); System.out.println("element: " + element); System.out.println("attribute: " + attribute); // 获取用户相关信息 Authentication authentication = attribute.getAuthentication(); UserDetails principal = (UserDetails) authentication.getPrincipal(); System.out.println(principal); System.out.println("username: " + principal.getUsername()); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?