2016-04-06 49 views
1

私は春のブートアプリケーションで春のセキュリティを使用しています。現在、ユーザー名とパスワードを使用してトークンを生成するオプションがあります。今私は携帯電話番号として電子メールとパスワードとしてユーザー名を欲しいというようなオプションを変更する必要があります。このように使用することは可能ですか?春のセキュリティのためのOAuth認証で認証フィールドを変更することはできますか?

答えて

0

はい、カスタム値を使用するオプションがあります。例えば、以下のコードをチェック

import org.springframework.beans.factory.annotation.Autowired; 
 
import org.springframework.security.core.authority.SimpleGrantedAuthority; 
 
import org.springframework.security.core.userdetails.User; 
 
import org.springframework.security.core.userdetails.UserDetails; 
 
import org.springframework.security.core.userdetails.UserDetailsService; 
 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 
 
import org.springframework.stereotype.Service; 
 

 
import sunbox.dao.IUserDAO; 
 
import java.util.ArrayList; 
 
import java.util.List; 
 

 
@Service 
 
public class CustomUserDetailsService implements UserDetailsService { 
 

 
\t @Autowired 
 
\t private IUserDAO iuserDAO; 
 

 
\t @Autowired 
 
\t public CustomUserDetailsService(IUserDAO iuserDAO) { 
 
\t \t this.iuserDAO = iuserDAO; 
 
\t } 
 

 
\t @SuppressWarnings({ "rawtypes", "unchecked" }) 
 
\t @Override 
 
\t public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { 
 
\t \t sunbox.domain.User usr = iuserDAO.getUserByEmail(s); \t \t 
 

 
\t \t if (usr == null) { 
 
\t \t \t throw new UsernameNotFoundException("User details not found with this username: " + s); 
 
\t \t } 
 

 
\t \t String username = usr.getEmail(); 
 
\t \t String password = usr.getMobile(); \t \t 
 
\t \t String role = usr.getUserType().getUserTypeName(); 
 

 
\t \t List authList = getAuthorities(role); 
 
\t \t \t 
 
\t \t User user = new User(username, password, authList);  
 
\t \t return user; 
 
\t } 
 

 
\t @SuppressWarnings({ "rawtypes", "unchecked" }) 
 
\t private List getAuthorities(String role) { 
 
\t \t List authList = new ArrayList(); 
 
\t \t authList.add(new SimpleGrantedAuthority("ROLE_USER"));  
 
\t \t if (role != null && role.trim().length() > 0) { 
 
\t \t \t if (role.equals("admin")) { 
 
\t \t \t \t authList.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
 
\t \t \t } 
 
\t \t }  
 
\t \t return authList; 
 
\t } 
 
}

関連する問題