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> <name>spring-security-login-and-registration</name>
<packaging>war</packaging> <packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version> <version>1.0.0-BUILD-SNAPSHOT</version>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.5.RELEASE</version> <version>1.1.6.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>

View File

@ -23,7 +23,6 @@ public class Role {
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
private User user; private User user;
@Column(name = "role") @Column(name = "role")
private Integer role; private Integer role;
@ -66,7 +65,7 @@ public class Role {
public void setRole(Integer role) { public void setRole(Integer role) {
this.role = role; this.role = role;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; 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.PasswordMatches;
import org.baeldung.validation.service.ValidEmail; import org.baeldung.validation.service.ValidEmail;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
@PasswordMatches @PasswordMatches
public class UserDto { public class UserDto {
@NotNull @NotNull
@ -23,6 +24,7 @@ public class UserDto {
@NotNull @NotNull
@NotEmpty @NotEmpty
private String email; private String email;
public String getEmail() { public String getEmail() {
return email; return email;
} }
@ -64,9 +66,11 @@ public class UserDto {
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public String getMatchingPassword() { public String getMatchingPassword() {
return matchingPassword; return matchingPassword;
} }
public void setMatchingPassword(String matchingPassword) { public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword; this.matchingPassword = matchingPassword;
} }

View File

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

View File

@ -83,7 +83,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
messageSource.setCacheSeconds(0); messageSource.setCacheSeconds(0);
return messageSource; return messageSource;
} }
@Bean @Bean
public EmailValidator usernameValidator() { public EmailValidator usernameValidator() {
EmailValidator userNameValidator = new EmailValidator(); 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.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({TYPE,ANNOTATION_TYPE}) @Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME) @Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchesValidator.class) @Constraint(validatedBy = PasswordMatchesValidator.class)
@Documented @Documented
public @interface PasswordMatches { public @interface PasswordMatches {
String message() default "Passwords don't match"; String message() default "Passwords don't match";
Class<?>[] groups() default {};
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {}; Class<? extends Payload>[] payload() default {};
} }

View File

@ -6,13 +6,14 @@ import javax.validation.ConstraintValidatorContext;
import org.baeldung.persistence.service.UserDto; import org.baeldung.persistence.service.UserDto;
public class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> { public class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> {
@Override @Override
public void initialize(PasswordMatches constraintAnnotation) { public void initialize(PasswordMatches constraintAnnotation) {
} }
@Override @Override
public boolean isValid(Object obj, ConstraintValidatorContext context){ public boolean isValid(Object obj, ConstraintValidatorContext context) {
UserDto user = (UserDto) obj; 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"?> <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" <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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd 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="/resources/**" access="permitAll" />
<intercept-url pattern="/invalidSession*" access="isAnonymous()" /> <intercept-url pattern="/invalidSession*" access="isAnonymous()" />
<intercept-url pattern="/**" access="isAuthenticated()" /> <intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page='/login.html' <form-login login-page='/login.html' authentication-failure-url="/login.html?error=true" authentication-success-handler-ref="myAuthenticationSuccessHandler"
authentication-failure-url="/login.html?error=true"
authentication-success-handler-ref="myAuthenticationSuccessHandler"
default-target-url="/homepage.html" /> default-target-url="/homepage.html" />
<session-management invalid-session-url="/invalidSession.html" <session-management invalid-session-url="/invalidSession.html" session-fixation-protection="none" />
session-fixation-protection="none" /> <logout invalidate-session="false" logout-success-url="/logout.html?logSucc=true" logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
<logout invalidate-session="false" logout-success-url="/logout.html?logSucc=true"
logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID" />
</http> </http>
<beans:bean id="myAuthenticationSuccessHandler"
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" /> <beans:bean id="myAuthenticationSuccessHandler" class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler" />
<authentication-manager> <authentication-manager>
<authentication-provider user-service-ref="userDetailsService"> <authentication-provider user-service-ref="userDetailsService" />
</authentication-provider>
</authentication-manager> </authentication-manager>
<beans:bean id="userDetailsService" class="org.baeldung.security.MyUserDetailsService" <beans:bean id="userDetailsService" class="org.baeldung.security.MyUserDetailsService" />
autowire="constructor" />
</beans:beans> </beans:beans>