forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XML Beans eliminated
- Loading branch information
Showing
51 changed files
with
7,825 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 25 additions & 1 deletion
26
...-security-login-and-registration/src/main/java/org/baeldung/spring/SecSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
package org.baeldung.spring; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.ImportResource; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
|
||
@Configuration | ||
@ComponentScan(basePackages = { "org.baeldung.security" }) | ||
@ImportResource({ "classpath:webSecurityConfig.xml" }) | ||
public class SecSecurityConfig { | ||
|
||
@Autowired | ||
UserDetailsService userDetailsService; | ||
|
||
public SecSecurityConfig() { | ||
super(); | ||
} | ||
|
||
} | ||
@Bean | ||
public BCryptPasswordEncoder encoder() { | ||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(11); | ||
return encoder; | ||
} | ||
|
||
@Bean | ||
public DaoAuthenticationProvider authProvider() { | ||
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); | ||
authProvider.setUserDetailsService(userDetailsService); | ||
authProvider.setPasswordEncoder(encoder()); | ||
return authProvider; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/org/baeldung/event/OnRegistrationCompleteEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.baeldung.event; | ||
|
||
import java.util.Locale; | ||
|
||
import org.baeldung.persistence.model.User; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@SuppressWarnings("serial") | ||
public class OnRegistrationCompleteEvent extends ApplicationEvent { | ||
|
||
private final String appUrl; | ||
private final Locale locale; | ||
private final User user; | ||
|
||
public OnRegistrationCompleteEvent(User user, Locale locale, String appUrl) { | ||
super(user); | ||
this.user = user; | ||
this.locale = locale; | ||
this.appUrl = appUrl; | ||
} | ||
|
||
public String getAppUrl() { | ||
return appUrl; | ||
} | ||
|
||
public Locale getLocale() { | ||
return locale; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/baeldung/event/listener/RegistrationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.baeldung.event.listener; | ||
|
||
import java.util.UUID; | ||
|
||
import org.baeldung.event.OnRegistrationCompleteEvent; | ||
import org.baeldung.persistence.model.User; | ||
import org.baeldung.persistence.service.IUserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.MessageSource; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class RegistrationListener implements ApplicationListener<OnRegistrationCompleteEvent> { | ||
@Autowired | ||
private IUserService service; | ||
|
||
@Autowired | ||
private MessageSource messages; | ||
|
||
@Autowired | ||
private JavaMailSender mailSender; | ||
|
||
@Override | ||
public void onApplicationEvent(OnRegistrationCompleteEvent event) { | ||
this.confirmRegistration(event); | ||
} | ||
|
||
private void confirmRegistration(OnRegistrationCompleteEvent event) { | ||
User user = event.getUser(); | ||
String token = UUID.randomUUID().toString(); | ||
service.createVerificationTokenForUser(user, token); | ||
|
||
String recipientAddress = user.getEmail(); | ||
String subject = "Registration Confirmation"; | ||
String confirmationUrl = event.getAppUrl() + "/regitrationConfirm.html?token=" + token; | ||
String message = messages.getMessage("message.regSucc", null, event.getLocale()); | ||
SimpleMailMessage email = new SimpleMailMessage(); | ||
email.setTo(recipientAddress); | ||
email.setSubject(subject); | ||
email.setText(message + " \r\n" + "http://localhost:8080" + confirmationUrl); | ||
mailSender.send(email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.baeldung.hashing; | ||
|
||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
public class HashGenerator { | ||
|
||
public String getHashedPassword(String password) { | ||
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | ||
String hashedPassword = passwordEncoder.encode(password); | ||
return hashedPassword; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/baeldung/persistence/dao/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.baeldung.persistence.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.baeldung.persistence.model.User; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
public User findByEmail(String email); | ||
|
||
public void delete(User user); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/baeldung/persistence/dao/VerificationTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.baeldung.persistence.dao; | ||
|
||
import org.baeldung.persistence.model.User; | ||
import org.baeldung.persistence.model.VerificationToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface VerificationTokenRepository extends JpaRepository<VerificationToken, Long> { | ||
|
||
public VerificationToken findByToken(String token); | ||
|
||
public VerificationToken findByUser(User user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.baeldung.persistence.model; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
|
||
@Entity | ||
@Table | ||
public class Role { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
private Integer role; | ||
|
||
public Role() { | ||
super(); | ||
} | ||
|
||
public Role(Integer role) { | ||
super(); | ||
this.role = role; | ||
} | ||
|
||
public Role(Integer role, User user) { | ||
super(); | ||
this.role = role; | ||
this.user = user; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public User getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(User user) { | ||
this.user = user; | ||
} | ||
|
||
public Integer getRole() { | ||
return role; | ||
} | ||
|
||
public void setRole(Integer role) { | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((role == null) ? 0 : role.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
final Role role = (Role) obj; | ||
if (!role.equals(role.role)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder builder = new StringBuilder(); | ||
builder.append("Role [role=").append(role).append("]").append("[id=").append(id).append("]"); | ||
return builder.toString(); | ||
} | ||
} |
Oops, something went wrong.