首页 > 解决方案 > 使用 Spring Security 和 Java Server Faces 成功登录后不显示用户名

问题描述

使用 Spring Security 和 JSF 成功登录后,我在“成功”页面上看不到“用户名”属性。“成功”页面如下所示:

<h:body>
    Welcome, #{users.username}
    <a href='j_spring_security_logout'>logout</a>
</h:body>

使用用户名“John”登录后,我希望看到“Welcome, John”,但文本是“Welcome, #{users.username}”。

登录表格为:

    <h:form id="loginForm" prependId="false">
        <!-- Messages must be global here, to show bad credentials -->
        <h:messages globalOnly="true"/>

        <!-- Id's must not be changed to support spring security check -->
        <h:panelGrid columns="3">
            <h:outputLabel for="j_username" value="User: * " />  
            <h:inputText id="j_username" required="true" label="username" value="#{users.username}" />  
            <h:message for="j_username"  style="color:red"/>

            <h:outputLabel for="j_password" value="Password: * " />  
            <h:inputSecret id="j_password" label="password" required="true" value="#{users.password}" />  
            <h:message for="j_password" style="color:red"/>

            <h:outputLabel for="_spring_security_remember_me" value="Remember me: " />
            <h:selectBooleanCheckbox    id="_spring_security_remember_me" />        
        </h:panelGrid>

        <h:commandButton type="submit" id="login" value="Login"
                         action="#{users.login}" />
    </h:form>

我使用 JSF 2.2,bean 类是这样的:

@ManagedBean
@Component
@SessionScoped
public class Users implements Serializable {
    private int id;
    private String username;
    private String password;
    private int enabled;

    @Autowired
    private LoginService loginService;

    // getters and setters

    public String login() throws ServletException, IOException {
        return loginService.doLogin();
}

标签: jsfspring-security

解决方案


推荐阅读