繁体   English   中英

如何在Javascript中使用Spring Security用户名

[英]How to use Spring Security username in Javascript

我试图在一些JavaScript中访问Spring Source Security用户变量$ {username},它会一直以空字符串形式返回。 在上一页上,我只用$ {username}显示用户名。 从我所知,我需要做的就是在javascript中获取这个:

<c:out value="${username}"/>

但是在下面的代码中,生成的HTML总是有一个空字符串。 我错过了什么?

<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Admin Page</title>
<style type="text/css">

    .wideCols .field-radio { width: 5%; }
    .wideCols .field-username { width: 15%; }
    .wideCols .field-firstName { width: 15%; }
    .wideCols .field-lastName { width: 20%; }
    .wideCols .field-email { width: 40%; }
    .wideCols .field-enabled { width: 5%; }
</style>
</head>
<body>
    <h1>Users</h1>
    <spring:url value="/admin/addUser" var="accountUrl" />

    <a href="${accountUrl}">Add User</a> 
    <div id="result1"></div>

    <div id="grid" class="wideCols"></div>
    <!-- <button type="button" id="saveNode"></button>  -->


    <script type="text/javascript">
        require([ "dojo/store/JsonRest",
                  "dojo/_base/declare",
                  "dgrid/OnDemandGrid",
                  "dgrid/Selection", 
                  "dgrid/editor",
                  "dgrid/selector",
                  "dojo/domReady!" 
                ], function(JsonRest, declare, OnDemandGrid, Selection, editor, selector) {

            var userAccountStore = new JsonRest({
                idProperty : "userAccountId",
                target : "<c:url value="/userMgr/" />"
            });

            window.grid = new (declare([OnDemandGrid, Selection]))({                
                store : userAccountStore,           
                columns:  [ selector({ label: 'radio'}, "radio"),
                            { label: "Username", field: "username"}, 
                            { label: "First Name", field: "firstName"}, 
                            { label: "Last Name", field: "lastName"}, 
                            { label: "Email", field: "email"}, 
                            editor({ label: "Enabled", field: "enabled", autoSave: "true", canEdit: function(object){
                                return object.username != "<c:out value="${username}"/>";
                            }, }, "checkbox")],
            }, "grid"); 

        });

    </script>
</body>
</html>

用户名不会自动添加到您的模型中。 您需要在控制器中执行此操作,或者如果您无权访问控制器代码,则可以在Spring Security标记库中使用不推荐使用的方法:

<sec:authentication property="principal.username" />

有关详细信息,请参阅http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6295

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM