首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为了春季安全,可以修改OAuth身份验证中的身份验证字段吗?

问为了春季安全,可以修改OAuth身份验证中的身份验证字段吗?
EN

Stack Overflow用户
提问于 2016-04-06 07:09:09
回答 1查看 33关注 0票数 1

我在我的spring引导应用程序中使用spring安全性。目前,我们可以选择使用用户名和密码生成令牌。现在,我需要修改选项,如我希望用户名作为电子邮件和密码作为移动号码。能不能用这样的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-06 07:12:44

是的,您可以选择使用自定义值。例如,检查下面的代码,

​

代码语言:javascript
复制
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 {  

	@Autowired
	private IUserDAO iuserDAO;

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

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

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

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

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

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

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36443895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档