small cleanup

This commit is contained in:
eugenp 2014-09-27 22:18:28 +03:00
parent eb55fdbaad
commit 686fdd8ee2
8 changed files with 34 additions and 33 deletions

View File

@ -7,11 +7,13 @@
<name>spring-security-login-and-registration</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.RELEASE</version>
<version>1.1.6.RELEASE</version>
</parent>
<dependencies>
<!-- Spring -->
<dependency>

View File

@ -23,7 +23,6 @@ public class Role {
@JoinColumn(name = "user_id")
private User user;
@Column(name = "role")
private Integer role;
@ -66,7 +65,7 @@ public class Role {
public void setRole(Integer role) {
this.role = role;
}
@Override
public int hashCode() {
final int prime = 31;

View File

@ -5,6 +5,7 @@ import javax.validation.constraints.NotNull;
import org.baeldung.validation.service.PasswordMatches;
import org.baeldung.validation.service.ValidEmail;
import org.hibernate.validator.constraints.NotEmpty;
@PasswordMatches
public class UserDto {
@NotNull
@ -23,6 +24,7 @@ public class UserDto {
@NotNull
@NotEmpty
private String email;
public String getEmail() {
return email;
}
@ -64,9 +66,11 @@ public class UserDto {
public void setPassword(String password) {
this.password = password;
}
public String getMatchingPassword() {
return matchingPassword;
}
public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword;
}

View File

@ -10,15 +10,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService implements IUserService {
public class UserService implements IUserService {
@Autowired
private UserRepository repository;
@Transactional
@Override
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
if (emailExist(accountDto.getEmail())) {
throw new EmailExistsException("There is an account with that email adress: " + accountDto.getEmail());
}
User user = new User();
@ -26,8 +25,8 @@ public class UserService implements IUserService {
user.setLastName(accountDto.getLastName());
user.setPassword(accountDto.getPassword());
user.setEmail(accountDto.getEmail());
//ROLE WILL ALWAYS BE USER. HARDCODING IT
user.setRole(new Role(Integer.valueOf(1),user));
// ROLE WILL ALWAYS BE USER. HARDCODING IT
user.setRole(new Role(Integer.valueOf(1), user));
return repository.save(user);
}

View File

@ -83,7 +83,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
messageSource.setCacheSeconds(0);
return messageSource;
}
@Bean
public EmailValidator usernameValidator() {
EmailValidator userNameValidator = new EmailValidator();

View File

@ -10,13 +10,15 @@ import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({TYPE,ANNOTATION_TYPE})
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchesValidator.class)
@Documented
public @interface PasswordMatches {
String message() default "Passwords don't match";
Class<?>[] groups() default {};
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -6,13 +6,14 @@ import javax.validation.ConstraintValidatorContext;
import org.baeldung.persistence.service.UserDto;
public class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> {
@Override
public void initialize(PasswordMatches constraintAnnotation) {
public void initialize(PasswordMatches constraintAnnotation) {
}
@Override
public boolean isValid(Object obj, ConstraintValidatorContext context){
public boolean isValid(Object obj, ConstraintValidatorContext context) {
UserDto user = (UserDto) obj;
return user.getPassword().equals(user.getMatchingPassword());
}
return user.getPassword().equals(user.getMatchingPassword());
}
}

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
@ -15,22 +14,17 @@
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/invalidSession*" access="isAnonymous()" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page='/login.html'
authentication-failure-url="/login.html?error=true"
authentication-success-handler-ref="myAuthenticationSuccessHandler"
<form-login login-page='/login.html' authentication-failure-url="/login.html?error=true" authentication-success-handler-ref="myAuthenticationSuccessHandler"
default-target-url="/homepage.html" />
<session-management invalid-session-url="/invalidSession.html"
session-fixation-protection="none" />
<logout invalidate-session="false" logout-success-url="/logout.html?logSucc=true"
logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
<session-management invalid-session-url="/invalidSession.html" session-fixation-protection="none" />
<logout invalidate-session="false" logout-success-url="/logout.html?logSucc=true" logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
</http>
<beans:bean id="myAuthenticationSuccessHandler"
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
<beans:bean id="myAuthenticationSuccessHandler" class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
<beans:bean id="userDetailsService" class="org.baeldung.security.MyUserDetailsService"
autowire="constructor" />
<beans:bean id="userDetailsService" class="org.baeldung.security.MyUserDetailsService" />
</beans:beans>