Skip to content

Latest commit

 

History

History

SpringBootSecProject

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Spring Security Project

# Importance of Security 
```markdown

- security : Authentication , Authorization ,  [use OWASP]
- performance : require the speed for any type of web
- stable app

OWASP Top 10

  • Open web Application Security Project

- 1. Broken Access Control 
- 2. Cryptographic failure
- 3. Injections 
- [SQL injections : user 'or 1=1' : use the prepared Statement] 
- 4. Insecure Design 
- 5 . Security Misconfigurations (like using the default the configurations)
- 6. Vulnerable and Outdated Componenets
- 7. Identtificationand Authentication Failure (encrypt the passwod when storing in db)
- 8. Software and Data Integrity Failures
- 9. Security Logging nad Monitoring Failures (avoiding the log due to perfromance can lead to issue)
- 10. Server-side Request Forgery (SSRF)

1. Default Login Form

# Spring Security -  
- The Spring Security add the one more layer of Spring Security with multiple filter and - then Dispatch Servlet -- then the Servlet sends the request to the particular Controller 

2. Spring Security Layers

- By default Filters : 

1. Filter Chain 
(Client -> Servlet Container -->Filter1-filter2 (They can call eachother for checking the logic or filtering )----> Servlet )

3. Session ID

@RestController
public class HelloController {

    @GetMapping("hello")
    public String greeting(HttpServletRequest request) {
        return "Hello World" +request.getSession().getId();
    }

    @GetMapping("about")
    public String about(HttpServletRequest request)
    {
        return "About Page " + request.getSession().getId();
    }

4. Getting Custom User Name and Password

5. CSRF :

5.1 CSRF Intro

  • When the user go the malicious website the website try to store the session id of the secure website and and this is called the cross site request forgery.

  • If every request is returning the token and next time when request is send need to submit the token as well

  • By request the Spring sec will implement the CSRF for the Post Put Update and Delete

5.2 Error Without CSRF Token

 {
    "id": 1,
    "name": "JP",
    "tech": "Rust"
  }
   401 Unauthorize

5.3 Sending the CSRF Token

// Getting the csrf-token by servlet 

   @GetMapping("csrf-token") 
    public CsrfToken getCsrfToken(HttpServletRequest request)
    {
            return (CsrfToken) request.getAttribute("_csrf");
    }
// TO send the post request add the csrf token in header with X-CSRF-TOKEN

5.4 Same Site Strict CSRF

  • this will not allow the cross site access
# application Properties : 
server.servlet.session.cookie.same-stie=strict

Making the Web Stateless

  • REST API :
    1. Stateful(using the same session id for the one login and subsequent req)
    1. Stateless : in this we need to login with the user name and password

5.5 Security Configuration

 @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception
    {  
        return http.build();
    }

6. Working With the Multiple Users

  • By default the spring security usages called the UserDetailsService to check for the applocation Properties for user and password

  • Defining the Own User Details Service

//    UserDetailService for the MultiUsers 
        @Bean
        public UserDetailsService userDetailsService()  // Spring will lookup for the obj serDetailsService to lookup the user data
        {
        //    UserDetails user = User.builder().build();
             @SuppressWarnings("deprecation")
            UserDetails user = User
                    .withDefaultPasswordEncoder()  //defaultencoder
                    .username("user")
                    .password("super123")
                    .roles("USER")
                    .build();

            @SuppressWarnings("deprecation")
            UserDetails admin = User
                .withDefaultPasswordEncoder()  //defaultencoder
                .username("Tor")
                .password("super123")
                .roles("ADMIN")
                .build();

            return new InMemoryUserDetailsManager(user, admin);
        }


}

7 Creating the User Table and DB Properties

# To connect the database using the JPA , add the dependencies 
- db driver
- jpa dependencies

# auth provider layer 

- need to add the authentication provider : ---> Authentication Object 
- auth provider will be connected to the db using the DAO (Data Access Object ) layer

8. Authentication Provider

// Bean to 
        private UserDetailsService userDetailsService;

        @SuppressWarnings("deprecation")
        @Bean
        public AuthenticationProvider authProvider() // setuserDetailsService is dependent on the authprovider
        {
            DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
            provider.setUserDetailsService(userDetailsService);
            // specify pass encoder
            provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance()); // to use the password encoder replace it 
            return provider;
        }

9. Creating User Repository using DAO

User Details and User Principal

- Authenticating the Current user in Spring Security called the User Principal 

10 . Bcrypt Intro

- using the Hashing you will get one hash for every hashed data : md5 , sha256 

- Can use the SHA256 for multiple times on the same data like 2^16 = times encrytion  

- bcrypt is the cryptography algo for the encrypting the password 

11. User Registration Controller

12. Bcrypt Encoding for User

@Service
public class UserService {

    @Autowired
    private UserRepo repo;
    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);

    public User saveUser(User user)
    {
        // setting the password with encoder
        user.setPassword(encoder.encode(user.getPassword()));
        
        System.out.println(user.getPassword());
        return  repo.save(user);  // using the Jpa
    }
}

13. Setting Password Encoder for authentication

 @Bean
        public AuthenticationProvider authProvider() // setuserDetailsService is dependent on the authprovider
        {
            DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
            provider.setUserDetailsService(userDetailsService);
            
            provider.setPasswordEncoder(new BCryptPasswordEncoder(12));

            return provider;
        }
// same strength in the creation and authentication 

13.1 Cross Origin

- for Cross origin backend run on the differnt origin or port number , and front-end on the different origin port.
- can apply on the particular controller or can apply in the application properties for whole controller.