标签:
1
|
< span style = "font-family:arial,
helvetica, sans-serif;" >< security:authentication-manager >< br >
< security:authentication-provider user-service-ref = "favUserDetailService" >< br >
</ security:authentication-provider >< br >
</ security:authentication-manager >< br >< br >
< bean id = "favUserDetailService" class = "com.favccxx.favsecurity.security.FavUserDetailService" />< br ></ span > |
1
|
<span
style= "font-family:arial,
helvetica, sans-serif;" > package com.favccxx.favsecurity.security;<br><br> import java.util.ArrayList;<br> import java.util.Collection;<br> import java.util.List;<br><br> import org.apache.logging.log4j.LogManager;<br> import org.apache.logging.log4j.Logger;<br> import org.springframework.security.core.GrantedAuthority;<br> import org.springframework.security.core.authority.SimpleGrantedAuthority;<br> import org.springframework.security.core.userdetails.User;<br> import org.springframework.security.core.userdetails.UserDetails;<br> import org.springframework.security.core.userdetails.UserDetailsService;<br> import org.springframework.security.core.userdetails.UsernameNotFoundException;<br><br> public class FavUserDetailService implements UserDetailsService
{<br><br> private static final Logger
logger = LogManager.getLogger(FavUserDetailService. class );<br><br> /**<br>
* 根据用户名获取用户 - 用户的角色、权限等信息<br> */ <br> public UserDetails
loadUserByUsername(String username)<br> throws UsernameNotFoundException
{<br> UserDetails userDetails = null ;<br> try {<br>
com.favccxx.favsecurity.pojo.User favUser = new com.favccxx.favsecurity.pojo.User();<br>
favUser.setUsername( "favccxx" );<br>
favUser.setPassword( "favccxx" );<br>
Collection<GrantedAuthority> authList = getAuthorities();<br> userDetails = new User(username,
favUser.getPassword().toLowerCase(), true , true , true , true ,authList);<br>
} catch (Exception
e) {<br> e.printStackTrace();<br> }<br><br><br> return userDetails;<br>
}<br><br> /**<br>
* 获取用户的角色权限,为了降低实验的难度,这里去掉了根据用户名获取角色的步骤<br> * @param <br> * @return<br> */ <br> private Collection<GrantedAuthority>
getAuthorities(){<br> List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
<br> authList.add( new SimpleGrantedAuthority( "ROLE_USER" ));
<br> authList.add( new SimpleGrantedAuthority( "ROLE_ADMIN" ));<br><br> return authList;<br>
}<br><br><br><br>}<br></span> |
Spring Security身份认证之UserDetailsService
标签:
原文地址:http://blog.csdn.net/shehun1/article/details/45394405