thymeleaf模板引擎使用过程中或许会有的问题,获取值的方式也是其中之一。
那么我就简单记录一下我在使用过程中遇到并解决问题所使用的方法。希望可以帮到大家!
首先讲一下页面的写法,实现使用了thymeleaf的话,那么thymeleaf的xmls是需要的;
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
然后页面就可以使用thymeleaf的th:标签属性了!
前端页面获取没有什么歧义,大家应该都知道,例如
<span th:text="${data.id}"></span>
其中要注意的是url的赋值需要用到@{},例如
<a th:href="@{${data.url}}" target="_blank" th:text="${data.name}"></a>
那么,JS怎么获取呢?重点来了,那就是“[[]]”,例如
<script th:inline="javascript">
var id = [[${data.id}]];
</script>
异步请求的时候需要写url也是一样,需要用到"[[]]";例如
<script type="text/javascript">
var ajaxUrl = "[[@{/test/path}]]";
</script>
使用spring-security是怎么获取session中的用户信息呢?
还有一些朋友会集成安全框架,比如spring的spring-security安全框架,使用这个框架的时候要获取后端session中存储的用户信息怎么获取呢?是这样的:
<span th:text="${session.SPRING_SECURITY_CONTEXT.authentication.userInfo.stUsnm}"></span>
这样就取到了对应session中存放的用户信息,当然,我后端是这样存放的:
我有一个userinfo的class(当然,大家可以扩展自己需要的信息)
public class UserInfo {
private String cdUs;
private String stLgnm;
private String stLgps;
private String stLgpsWjm;
private String stUsnm;
private String cdDp;
private String stDpnm;
private String cdRl;
private String stRonm;
private String nmFlag;
public String getStLgpsWjm() {
return stLgpsWjm;
}
public void setStLgpsWjm(String stLgpsWjm) {
this.stLgpsWjm = stLgpsWjm;
}
public String getNmFlag() {
return nmFlag;
}
public void setNmFlag(String nmFlag) {
this.nmFlag = nmFlag;
}
public String getCdUs() {
return cdUs;
}
public void setCdUs(String cdUs) {
this.cdUs = cdUs;
}
public String getStLgnm() {
return stLgnm;
}
public void setStLgnm(String stLgnm) {
this.stLgnm = stLgnm;
}
public String getStLgps() {
return stLgps;
}
public void setStLgps(String stLgps) {
this.stLgps = stLgps;
}
public String getStUsnm() {
return stUsnm;
}
public void setStUsnm(String stUsnm) {
this.stUsnm = stUsnm;
}
public String getCdDp() {
return cdDp;
}
public void setCdDp(String cdDp) {
this.cdDp = cdDp;
}
public String getStDpnm() {
return stDpnm;
}
public void setStDpnm(String stDpnm) {
this.stDpnm = stDpnm;
}
public String getCdRl() {
return cdRl;
}
public void setCdRl(String cdRl) {
this.cdRl = cdRl;
}
public String getStRonm() {
return stRonm;
}
public void setStRonm(String stRonm) {
this.stRonm = stRonm;
}
}
然后,我继承了security的UsernamePasswordAuthenticationToken类作为数据的存储(这个也可以在里面扩展自己需要的与用户有关联的一些信息)
public class SecurityUser extends UsernamePasswordAuthenticationToken {
private static final long serialVersionUID = 1L;
private UserInfo userInfo;
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public SecurityUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
public SecurityUser(String username, String password, UserInfo userinfo, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
this.userInfo = userinfo;
}
}
再然后,我实现了AuthenticationProvider接口的验证信息,并存放用户的信息
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private HttpSession session;
@Autowired
private UserService userService;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
// 检查用户名密码是否正确
UserInfo user = userService.loadUserByUsernameAndPassword(username, MD5Util.encryption(password));
if (user == null) {
logger.error("{} 登录失败", username);
throw new BadCredentialsException("用户名或密码错误!");
} else if ("1".equals(user.getNmFlag())) { //验证是否可用
throw new AccountExpiredException("用户已锁定!");
}
user.setStLgpsWjm(password);
Authentication auth = new SecurityUser(username, password, user, grantedAuthorities);
return auth;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
这样一来,验证通过后,前端页面就可以通过session.SPRING_SECURITY_CONTEXT.authentication.userInfo.stUsnm获取到了。聪明的朋友应该都看出来了,userInfo对应的是我SecurityUser的class中的用户信息属性。
当然,js中也可以获取
<script th:inline="javascript">
var loginUser = [[${session.SPRING_SECURITY_CONTEXT.authentication.userInfo}]];
</script>
这样就获取到了对应的登录用户的信息