Ond*_*kar 3 java spring spring-security spring-boot
我找到了一些关于Spring Security登录和注册的教程:
http://www.baeldung.com/spring-security-login-error-handling-localization
http://www.baeldung.com/registration-with-spring-mvc-and-spring-security
但是,我现在正在使用,Spring Boot并希望找到该教程.问题是,我能找到的只是OAuth2和SSO
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/(第28点)
如果我想实现一个简单的登录和注册表单,任何想法如何继续Spring Boot?
编辑:我在官方文档中找到了这个.那会有登录功能,但是注册呢?
您需要UserDetailsService来存储和接收用户详细信息,当然还需要至少1个服务来实际存储用户.
例如这项服务:
@Service
public class RegistrationService {
@Resource
private MailService mailer;
@Resource
private UserDataService userDataService;
public boolean createRegistration(RegistryCredentials credentials,
MessageContext context) {
String username = credentials.getUsername();
UserData current = new UserData();
if (userDataService.isAvailable(username)) {
userDataService.addUserData(username, current);
return true;
} else {
context.addMessage(new MessageBuilder().error().source("username")
.code("username.exists").build());
return false;
}
}
public void saveUserData(RegistryCredentials credentials) {
UserData current = userDataService.getUserData(credentials
.getUsername());
current.setCity(credentials.getCity());
current.setEmail(credentials.getEmail());
current.setPassword(credentials.getPassword());
current.setStreet(credentials.getStreet());
current.setFirstname(credentials.getFirstname());
current.setLastname(credentials.getLastname());
current.setUsername(credentials.getUsername());
current.setRegistered(new Date());
Long activationKey = userDataService.updateUserData(current);
mailer.sendSubscriptionEmail(current, activationKey);
}
}
Run Code Online (Sandbox Code Playgroud)
RegistryCredentials是包含用户数据字段和验证注释的标准bean.您在注册页面上创建registryCredentials,将它们签入服务,然后服务将创建(并保留)用户详细信息.
当然,您必须使用SpringSecurity注册自己的服务实现,因此您需要在安全配置中使用它:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
UserDataService userDataService) throws Exception {
auth.userDetailsService(userDataService);
}
Run Code Online (Sandbox Code Playgroud)
对于简单的内存中解决方案,您可以使用如下的UserDataService:
@Service
public class UserDataService implements UserDetailsService {
private HashMap<String, UserData> userData = new HashMap<>();
private HashMap<Long, UserData> activation = new HashMap<>();
public UserDataService() {
UserData admin = new UserData();
admin.setUsername("admin");
admin.setPassword("password");
admin.setActive(true);
userData.put("admin", admin);
}
public boolean isAvailable(String username) {
return !userData.containsKey(username);
}
public void addUserData(String username, UserData data) {
userData.put(username, data);
}
public UserData getUserData(String username) {
return userData.get(username);
}
public Long updateUserData(UserData changed) {
userData.put(changed.getUsername(), changed);
Long random = new SecureRandom().nextLong();
activation.put(random, changed);
return random;
}
public UserData activateAccount(Long key) {
return activation.remove(key);
}
public boolean mayActivate(Date d, UserData data) {
long day = 86400000L;
Date reg = data.getRegistered();
reg.setTime(reg.getTime() + day);
if (d.before(reg)) {
return true;
} else {
return false;
}
}
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
UserData user = getUserData(username);
if (user != null) {
return user;
}
throw new UsernameNotFoundException("Username not found");
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,重要的是UserData必须实现UserDetails并且Principal安全认证实际上能够使用它.
此特定解决方案创建了在首次登录之前使用的注册令牌,但这完全是可选的,因为您可以根据需要配置登录过程.
| 归档时间: |
|
| 查看次数: |
2500 次 |
| 最近记录: |