gpt4 book ai didi

jsf - 如何获取 j_security_check 的用户名作为 bean 属性

转载 作者:行者123 更新时间:2023-12-05 00:45:48 25 4
gpt4 key购买 nike

在我的托管 bean 中,我希望获取 j_security_check 用户名,它授权给 LDAP 服务器,作为 bean 属性。

本质上,我想从中提取用户名

<input type="text" name="j_username" />

一旦提交并全部授权并在以下情况中使用它:

@Override
public String getName() {
return getId();
}

我将如何使用

FacesContext.getExternalContext().getUserPrincipal();

获取用户名作为 bean 属性?

如果您需要知道正在做什么,这是完整的支持 bean。我曾经手动让用户在文本框中输入用户名,现在我想停止这种情况并让它自动提取用户名。

//@Named("user")
@SessionScoped
public class UserBean implements Serializable, Principal {

private static final long serialVersionUID = 1L;
@NotNull(message = "The username can not be blank")
@Size(min = 6, max = 12, message = "Please enter a valid username (6-12 characters)")
//@Pattern(regexp = "[a-zA-Z0-9_]", message = "Please enter a valid username consiting of only characters that are from the alphabet or are numeric ")
//@Pattern(regexp = "(a-z)(A-Z)(0-9))", message = "Please enter a valid username consiting of only characters that are from the alphabet or are numeric ")
private String id;

public String getId() {
return id;
}

public void setId(String newValue) {
id = newValue;
}
private String fileText;

@NotNull(message = "You must select a file to upload")
public String getFileText() {
return fileText;
}

public void setFileText(String fileText) {
this.fileText = fileText;
}

/**
* public void getName(HttpServletRequest req, HttpServletResponse res)
* throws ServletException, java.io.IOException { id = req.getRemoteUser();
* }
*/
@Override
public String getName() {
return getId();
}
/*
* @Override
*
* public String request.remoteUser() { return getId();
*
* }
* .
*/
}

最佳答案

@PostConstruct中初始化它。

private String username;

@PostConstruct
public void init() {
username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
}

或者,如果您只在处理表单提交时需要它,只需在 action 方法中获取它即可。

请注意,getRemoteUser() 基本上返回与 getUserPrincipal().getName() 相同的结果。


与具体问题无关:这种 bean 不应在 session 范围内,而应在 View 或 session 范围内。此外,它不应实现 Principal 接口(interface)。这完全没有道理。

关于jsf - 如何获取 j_security_check 的用户名作为 bean 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14462064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com