diff --git a/security-modules/cas/cas-secured-app/pom.xml b/security-modules/cas/cas-secured-app/pom.xml
index 6f1ec22cb5..6e1b878354 100644
--- a/security-modules/cas/cas-secured-app/pom.xml
+++ b/security-modules/cas/cas-secured-app/pom.xml
@@ -36,6 +36,15 @@
spring-boot-devtools
runtime
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ mysql
+ mysql-connector-java
+ runtime
+
org.springframework.boot
spring-boot-starter-test
diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/CasSecuredApplication.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/CasSecuredApplication.java
index 62da9cf725..9293a3e00f 100644
--- a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/CasSecuredApplication.java
+++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/CasSecuredApplication.java
@@ -6,6 +6,7 @@ import org.jasig.cas.client.validation.Cas30ServiceTicketValidator;
import org.jasig.cas.client.validation.TicketValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@@ -34,6 +35,7 @@ public class CasSecuredApplication {
SpringApplication.run(CasSecuredApplication.class, args);
}
+
@Bean
public CasAuthenticationFilter casAuthenticationFilter(
AuthenticationManager authenticationManager,
@@ -58,6 +60,10 @@ public class CasSecuredApplication {
return new Cas30ServiceTicketValidator("https://localhost:8443/cas");
}
+ @Bean
+ public MyUserDetailsService getUser(){
+ return new MyUserDetailsService();
+ }
@Bean
public CasAuthenticationProvider casAuthenticationProvider(
TicketValidator ticketValidator,
@@ -65,9 +71,10 @@ public class CasSecuredApplication {
CasAuthenticationProvider provider = new CasAuthenticationProvider();
provider.setServiceProperties(serviceProperties);
provider.setTicketValidator(ticketValidator);
- provider.setUserDetailsService(
+ /* provider.setUserDetailsService(
s -> new User("casuser", "Mellon", true, true, true, true,
- AuthorityUtils.createAuthorityList("ROLE_ADMIN")));
+ AuthorityUtils.createAuthorityList("ROLE_ADMIN")));*/
+ provider.setUserDetailsService(getUser());
provider.setKey("CAS_PROVIDER_LOCALHOST_8900");
return provider;
}
diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/MyUserDetailsService.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/MyUserDetailsService.java
new file mode 100644
index 0000000000..35c83bfd75
--- /dev/null
+++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/MyUserDetailsService.java
@@ -0,0 +1,34 @@
+package com.baeldung.cassecuredapp;
+
+import java.util.Collections;
+
+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;
+
+public class MyUserDetailsService implements UserDetailsService {
+
+ @Autowired
+ private UserRepository userRepository;
+
+ @Override
+ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
+ // Get the user from the database.
+ Users users = getUserFromDatabase(username);
+
+ // Create a UserDetails object.
+ UserDetails userDetails = new User(
+ users.getEmail(),
+ users.getPassword(),
+ Collections.singletonList(new SimpleGrantedAuthority("ROLE_ADMIN")));
+
+ return userDetails;
+ }
+
+ private Users getUserFromDatabase(String username) {
+ return userRepository.findByEmail(username);
+ }
+}
diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/UserRepository.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/UserRepository.java
new file mode 100644
index 0000000000..366329a3fb
--- /dev/null
+++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/UserRepository.java
@@ -0,0 +1,15 @@
+package com.baeldung.cassecuredapp;
+
+import java.util.Optional;
+
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.CrudRepository;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface UserRepository extends CrudRepository {
+
+ Users findByEmail(@Param("email") String email);
+
+}
diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/Users.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/Users.java
new file mode 100644
index 0000000000..4e775e80e3
--- /dev/null
+++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/Users.java
@@ -0,0 +1,41 @@
+package com.baeldung.cassecuredapp;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class Users {
+ @Id
+ private Long id;
+
+ @Column(nullable = false, unique = true)
+ private String email;
+
+ private String password;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+}
diff --git a/security-modules/cas/cas-secured-app/src/main/resources/application.properties b/security-modules/cas/cas-secured-app/src/main/resources/application.properties
index f8789997d5..fa13527982 100644
--- a/security-modules/cas/cas-secured-app/src/main/resources/application.properties
+++ b/security-modules/cas/cas-secured-app/src/main/resources/application.properties
@@ -1,2 +1,8 @@
server.port=8900
-spring.freemarker.suffix=.ftl
\ No newline at end of file
+spring.freemarker.suffix=.ftl
+
+spring.jpa.generate-ddl=false
+spring.datasource.url= jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
+spring.datasource.username=root
+spring.datasource.password=root1234
+spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
\ No newline at end of file