Spring SecurityはSessionの値を取り、userDetails(zz)を修正します.
5854 ワード
1.sessionでspring securityを取得するログイン名は以下の通りです.
$
spring securityがSPRING_SECURITY_CONTEXTにsessionを入れましたが、直接usernameを入れませんでした.
次のコードは主にsessionに保存されている変数です.
URL
session {SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}
session
session {SPRING_SECURITY_CONTEXT=org.springframework.security.context.SecurityContextImpl@87b16984: Authentication: org.springframework.security.providers.cas.CasAuthenticationToken@87b16984: Principal: com.avi.casExtends.UserInfo@ff631d80: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Password: [PROTECTED]; Authenticated: true; Details: org.springframework.security.ui.WebAuthenticationDetails@12afc: RemoteIpAddress: 127.0.0.1; SessionId: AE56E8925195DFF4C50ABD384574CCEA; Granted Authorities: ROLE_ADMIN Assertion: org.jasig.cas.client.validation.AssertionImpl@661a11 Credentials (Service/Proxy Ticket): ST-3-1lX3acgZ6HNgmhvjXuxB-cas, userId=2, userName=test}
2.ページの端に使ってください.
<%@taglib prefix='security'uri='http://www.springframework.org/security/tags'%
<security:authentication property=「printipad.username」><security:authentication>
または
<security:authorize ifAllGrand=「ROLE ADMIN」>
またはセッション中の値を取る
ヽoo.ツ..........................................................SECURITY_CONTEXT.authentication.printipad.usernameは
3.バックグラウンドで取得する
UserDetails userDetails=(UserDetails)SecurityContect Holder.get Conttext() .get Authentication() .get Principal()
userDetails.get Username()
より多くの情報を得るためには、userDetailsのデフォルトの実装クラスとUserDetails Serviceインターフェースを拡張します.
springsecurityは全体のuser情報をsessionに入れるものです.すなわち、session.SPRING_SECURITY_CONTEXT.authentication.printipad
これはアメリカを代表する対象です.
そこで私は拡張をしてuserの中の情報を増加してuserIdを加えました.
コードは以下の通りです.拡張user
package com.avi.casExtends;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.User;
public class UserInfo extends User{
private static final long serialVersionUID = 1L;
private String userId;
@SuppressWarnings("deprecation")
public UserInfo(String username, String password, boolean enabled, GrantedAuthority[] authorities)
throws IllegalArgumentException {
super(username,password, enabled, authorities);
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}
userDetailsserviceインターフェースを実現します.
package com.avi.casExtends;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import com.avi.dao.AccountDao;
import com.avi.data.User;
public class UserInfoService implements UserDetailsService{
private AccountDao accountDao;
private Map<String, UserInfo> userMap = null;
public UserInfoService() {
}
public void fillMap(){
userMap = new HashMap<String, UserInfo>();
List<User> users = accountDao.findAllUsers();
UserInfo userInfo = null;
for(User user:users){
userInfo = new UserInfo(user.getUserName(),user.getPassword(),true,new GrantedAuthority[]{
new GrantedAuthorityImpl(user.getRole()),
});
userInfo.setUserId(user.getId().toString());
userMap.put(user.getUserName(), userInfo);
}
}
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
if(userMap==null)
fillMap();
return userMap.get(username);
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public Map<String, UserInfo> getUserMap() {
return userMap;
}
public void setUserMap(Map<String, UserInfo> userMap) {
this.userMap = userMap;
}
}
prvate Acceount Dao account Dao;注入した検索データベースの種類です.
XMLファイルの指定に使用するserviceを修正します.
<authentication-provider user-service-ref="userDetailsService"/>
<bean id="userDetailsService" class="com.avi.casExtends.UserInfoService" singleton="false">
<property name="accountDao" ref="accountDao"/>
</bean>
$
http://blog.csdn.net/chenyongsuda/article/details/5327059