從後臺Controller中獲取用戶名javascript
從SecurityContextHolder中取出用戶名(或密碼),放到map集合中,返回map集合html
SecurityContextHolder.getContext().getAuthentication().getName();前端
@Controller @RequestMapping("/login") public class LoginController { @RequestMapping("/showName") @ResponseBody public Map loginShowName(){ String name = SecurityContextHolder.getContext().getAuthentication().getName(); Map map = new HashMap(); map.put("username",name); return map; } }
前端代碼java
html中書寫樣式angularjs
base.jsspring
// 定義模塊:
var app = angular.module("pinyougou",[]);
indexController.jsapp
app.controller("indexController",function($scope,loginService){ $scope.showName = function(){ loginService.showName().success(function(response){
//username是從後臺map集合返回的key值,保存到loginName中 $scope.loginName = response.username; }); } });
loginService.jsthis
app.service("loginService",function($http){ this.showName = function(){ return $http.get("../login/showName.do"); } });
html頁面中先引入jsurl
<!-- 引入angular的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/controller/indexController.js"></script>
<script type="text/javascript" src="../js/service/loginService.js"></script>
在body上添加spa
ng-app="pinyougou" ng-controller="indexController" ng-init="showName()"
指定初始化方法showName()
在頁面中經過{{loginName}}取值
在用戶註銷的超連接上,添加 href="../logout"
在spring-security.xml中添加註銷
<!-- 註銷的配置,註銷成功跳轉login.html頁面 -->
<logout logout-url="/logout" logout-success-url="/login.html" />