2016-06-28 135 views
0

我有手動驗證春季安全如下代碼:手動驗證春季安全

Authentication authentication = new UsernamePasswordAuthenticationToken(username, password); 

SecurityContextHolder.getContext().setAuthentication(authenticationManager.authenticate(authentication)); 

在這些代碼中,場usernamepassword是在我的系統真正的賬戶。

但是將真實信息放在代碼中很敏感。

我的意思是,我們創建了一個假帳戶,並授予他們權力,如USERADMIN

我的問題是:

反正有人工驗證,而不使用真實賬戶。

+0

用戶名和密碼分別是「usernameReal」和「passwordReal」。對於那個很抱歉! –

回答

1

是的,只要創建一個Username... Authentication用任何用戶名和密碼,只要他們不是真實的,再加上所需的權限。

然後把它放在SecurityContext

List<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN")); 
Authentication authentication = new UsernamePasswordAuthenticationToken("admin_user", "", authorities); 
SecurityContextHolder.getContext().setAuthentication(authentication); 
+0

我試過你的方式,但我發現一個問題,因爲:不能設置此令牌信任 - 使用構造函數,它需要一個GrantedAuthority列表 –

+0

嗯,它的工作原理。謝謝 –