spring security+freemarker获取登陆用户的信息
spring security+freemarker获取登陆用户的信息
目标页面之间获取
${Session.SPRING_SECURITY_CONTEXT.authentication.principal.username}
其他参考
Spring Security判断用户是否已经登录
<c:if test="${pageContext.request.userPrincipal.name != null}">
<label>
Hi ${pageContext.request.userPrincipal.name} ! Welcome to our site
</label>
</c:if>
<c:choose>
<c:when test="${pageContext.request.userPrincipal.authenticated}">Show something</c:when>
<c:otherwise>Show something else</c:otherwise>
</c:choose>
方法二、检查角色
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <sec:authorize access="hasAnyAuthority('ROLE_ADMIN', 'ROLE_USER')" var="isAuthenticated">
</sec:authorize> <c:out value="${isAuthenticated}"/>
和这个
<sec:authorize access="hasAnyRole('ROLE_ADMIN')">
<a href="delete/${file.id}">Delete</a>
</sec:authorize>
方法三、 还是查询用户
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
// do something...
}
方法四、 使用标签库
<%@taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<sec:authorize access="isAuthenticated()">
<% response.sendRedirect("main"); %>
</sec:authorize>
方法五、 使用注解
需要:<global-method-security secured-annotations="enabled" />
@Secured("ROLE_ADMIN")
@RequestMapping(params = "onlyForAdmins")
public ModelAndView onlyForAdmins() {
....
}
@PreAuthorize("isAuthenticated()")
@RequestMapping(params = "onlyForAuthenticated")
public ModelAndView onlyForAuthenticatedUsers() {
....
}
方法六、 编程
SecurityContextHolder.getContext().getAuthentication() != null &&
SecurityContextHolder.getContext().getAuthentication().isAuthenticated() &&
//when Anonymous Authentication is enabled
!(SecurityContextHolder.getContext().getAuthentication()
instanceof AnonymousAuthenticationToken) if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
System.out.println("LOGGED IN");
} else {
System.out.println("NOT LOGGED IN");
} if (!SecurityContextHolder.getContext().getAuthentication().getName().
equals("anonymousUser")) {
System.out.println("LOGGED IN");
} else {
System.out.println("NOT LOGGED IN");
}
https://www.jianshu.com/p/70569b64f9a9
https://blog.csdn.net/when_where_who/article/details/53610345
spring security+freemarker获取登陆用户的信息的更多相关文章
- SpringBoot Spring Security 核心组件 认证流程 用户权限信息获取详细讲解
前言 Spring Security 是一个安全框架, 可以简单地认为 Spring Security 是放在用户和 Spring 应用之间的一个安全屏障, 每一个 web 请求都先要经过 Sprin ...
- Spring Boot+Spring Security:获取用户信息和session并发控制
说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...
- spring security LDAP获取用户信息
很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-7.授权登录获取微信用户个人信息实战
笔记 7.授权登录获取微信用户个人信息实战 简介:讲解使用授权码code获取用户个人信息接口 关键点:看微信文档,字段尽量用拷贝 1.通过code获取access_token ...
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- Dynamics 365客户端编程示例:获取当前用户的信息,表单级通知/提示,表单OnLoad事件执行代码
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 关于Spring Security 3获取用户信息的问题
标签: spring security 3标签获取用户信息 2013-01-05 10:40 5342人阅读 评论(0) 收藏 举报 分类: Spring(25) java(70) 前端(7) ...
- Spring Security教程(八):用户认证流程源码详解
本篇文章主要围绕下面几个问题来深入源码: 用户认证流程 认证结果如何在多个请求之间共享 获取认证用户信息 一.用户认证流程 上节中提到Spring Security核心就是一系列的过滤器链,当一个请求 ...
- Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息
[Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...
随机推荐
- 转:网卡、光纤网卡、ISCSI卡有什么区别?
网卡.光纤网卡.ISCSI卡有什么区别? 感谢: http://www.it.com.cn/f/server/063/6/241650.htm 在讨论这个问题的时候,需要先说清楚一个问题:我们知道,在 ...
- 【Python042--魔法方法:算术运算】
一.算术魔法方法的举例 1.加法(__add__)的算术运算调用减法(__sub__)的算术运算,减法(__sub__)的算术运算调用加法(__add__)的算术运算 class New_Init(i ...
- topcoder srm 430 div1
problem1 link 其实就是找到一个数字$t$,使得$x$的二进制为1 的位上$t$也都为1.然后$t$删掉所有那些$x$为1的二进制位就是$k$. problem2 link 设所有合法的边 ...
- emmc和ssd的区别【转】
本文转载自:https://blog.csdn.net/hawk_lexiang/article/details/78228789 emmc和ssd eMMC和SSD主要是满足不同需求而发展出来的NA ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)
题目要求很简单,做法很粗暴 直接扫一遍即可 注意结果会爆int #include <cstdio> #include <algorithm> #include <cstr ...
- (转载)winform图片标尺控件
最近要做个winform的东西,要在里面集成一个类似Windows自带画图的标尺功能,还要能在图片上画矩形框.在网上找了好久也没找到写好的控件,无奈自己做了一个. 目前还有些bug,这里先做个分享.( ...
- SHOI 2017 相逢是问候(扩展欧拉定理+线段树)
题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...
- RabbitMQ学习之延时队列
原帖参考:http://www.cnblogs.com/telwanggs/p/7124687.html?utm_source=itdadao&utm_medium=referral http ...
- CPU核数和load average的关系
在前面的文章<Linux系统监控——top命令>中我简单提到了,判断load average的数值到底大不大的判断依据,就是数值除以CPU核数,大于5,就说明超负荷运转了.——这里其实不太 ...
- 1.spring基础知识讲解
引言:以下记录一些自己在使用时pringle框架时的一些自己心得合成体会时,如有侵权,请联系本博主. 1. spring基本都使用 spring是一个开源的轻量级框架,其目的是用于简化企业级应用程序开 ...