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 84bbd2ace4..f78d7a45d5 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 @@ -12,6 +12,8 @@ import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.cas.ServiceProperties; import org.springframework.security.cas.authentication.CasAuthenticationProvider; import org.springframework.security.cas.web.CasAuthenticationFilter; +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.User; import org.springframework.security.web.authentication.logout.LogoutFilter; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; @@ -62,10 +64,11 @@ 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")));*/ - provider.setUserDetailsService(getUser()); + AuthorityUtils.createAuthorityList("ROLE_ADMIN"))); + //For Authentication with a Database-backed UserDetailsService + //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/service/CasUserDetailsService.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/service/CasUserDetailsService.java index ac4fef1ff0..801f5726b7 100644 --- a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/service/CasUserDetailsService.java +++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/service/CasUserDetailsService.java @@ -4,11 +4,12 @@ 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; -import com.baeldung.cassecuredapp.user.User; +import com.baeldung.cassecuredapp.user.CasUser; import com.baeldung.cassecuredapp.user.UserRepository; public class CasUserDetailsService implements UserDetailsService { @@ -19,18 +20,18 @@ public class CasUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // Get the user from the database. - User user = getUserFromDatabase(username); + CasUser casUser = getUserFromDatabase(username); // Create a UserDetails object. - UserDetails userDetails = new org.springframework.security.core.userdetails.User( - user.getEmail(), - user.getPassword(), + UserDetails userDetails = new User( + casUser.getEmail(), + casUser.getPassword(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_ADMIN"))); return userDetails; } - private User getUserFromDatabase(String username) { + private CasUser getUserFromDatabase(String username) { return userRepository.findByEmail(username); } } diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/User.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/CasUser.java similarity index 96% rename from security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/User.java rename to security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/CasUser.java index 1dddc11458..2bf96d0994 100644 --- a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/User.java +++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/CasUser.java @@ -5,7 +5,7 @@ import javax.persistence.Entity; import javax.persistence.Id; @Entity -public class User { +public class CasUser { @Id private Long id; diff --git a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/UserRepository.java b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/UserRepository.java index 0481fabb06..623ccf2e38 100644 --- a/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/UserRepository.java +++ b/security-modules/cas/cas-secured-app/src/main/java/com/baeldung/cassecuredapp/user/UserRepository.java @@ -4,11 +4,9 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; -import com.baeldung.cassecuredapp.user.User; - @Repository -public interface UserRepository extends CrudRepository { +public interface UserRepository extends CrudRepository { - User findByEmail(@Param("email") String email); + CasUser findByEmail(@Param("email") String email); } 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 fa13527982..5c93c67ff1 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,8 +1,8 @@ server.port=8900 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 +#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=root +#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver \ No newline at end of file