View SecurityDemoLoginPage.html
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
<!DOCTYPE html > | |
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" | |
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> | |
<head > | |
<title>Spring Security Example </title> | |
</head> | |
<body> | |
<div th:if="${param.error}"> | |
Invalid username and password. | |
</div> |
View SecurityDemoSpringSecurityApplication.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
package com.raj; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
@SpringBootApplication | |
public class SpringSecurityApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(SpringSecurityApplication.class, args); |
View SecurityDemoMyUserDelailsService.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
package com.raj.service; | |
import com.raj.model.User; | |
import com.raj.repository.UserRepository; | |
import com.raj.securingweb.UserPrincipal; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
import org.springframework.stereotype.Service; |
View SecurityDemoWebSecurityConig.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
package com.raj.securingweb; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.authentication.AuthenticationProvider; | |
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
View SecurityDemoUserPrincipal.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
package com.raj.securingweb; | |
import com.raj.model.User; | |
import org.springframework.security.core.GrantedAuthority; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import java.util.Collection; | |
import java.util.Collections; |
View SecurityDemoUserRepo.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
package com.raj.repository; | |
import com.raj.model.User; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface UserRepository extends JpaRepository<User, Long> { | |
User findByEmail(String email); | |
} |
View SecurityDemoController.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
package com.raj.controller; | |
import org.springframework.security.authentication.AnonymousAuthenticationToken; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
import org.springframework.web.bind.annotation.GetMapping; | |
@org.springframework.stereotype.Controller | |
public class Controller { |
View SecurityDemoUser.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
package com.raj.model; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name = "users") | |
public class User { |
View gist:f8c86fe1b1527863bd86c1244ae971b1
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.4.2</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> |
View SecurityDemoApplication.properties
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
spring.datasource.url=jdbc:postgresql://localhost:5432/test | |
spring.datasource.username=postgres | |
spring.datasource.password=password | |
spring.jpa.generate-ddl=true | |
spring.jpa.show-sql=true | |
spring.jpa.properties.hibernate.format_sql=true | |
NewerOlder