登录显示登录名spring-security

登录时显示登录名

1.编写后台代码LoginController.java
package com.xxx.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
    @RequestMapping("/login")
    public class LoginController {

        @RequestMapping("/name")
        public Map name(){
            String name = SecurityContextHolder.getContext().getAuthentication().getName();
            Map map = new HashMap();
            map.put("loginName", name);
            return map;
        }

    }

2.编写前端代码,引入loginService.js

app.service('loginService',function($http){
    this.loginName=function(){
        return $http.get("../login/name.do");
    }
})

3.引入indexController.js

app.controller('indexController',function($scope,loginService){

    $scope.showLoginName = function(){
        loginService.loginName().success(function(response){
            $scope.loginName = response.loginName;
        })
    }
})

4.引入base.js

var app=angular.module('xxx',[]);

5.在主页面里引入js

<script type="text/javascript" src="./plugins/angularjs/angular.min.js">script>
<script type="text/javascript" src="./js/base.js">script>
<script type="text/javascript" src="./js/service/loginService.js">script>
<script type="text/javascript" src="./js/controller/indexController.js">script> 

6.在body里添加模板ng-app,控制器ng-controller,初始化ng-init

<body ng-app="xxx" ng-controller="indexController" ng-init="showLoginName()">

7.使用angularjs表达式引入用户名

<span class="name">{{loginName}}span>

你可能感兴趣的:(spring-security)