Theory of Springsecurity
Theory of Springsecurity
config,controller,service,security,repository,entity,payload etc
like:-
private AppUserRepository appUserRepository;
Now, If we again try to register the user with same Username or with same email
address than it will show us an error but we don't want to see that error instead
of that we want to see a message that user exists or email exists.
Now, after add the method in repository we need to go to controller layer and make
a coditions their
if(appUserRepository.existsByUsername(user.getUsername()){
return new ResponseEntity<>("Username
exists",HttpStatus.Bad_Request);
}
AppUser createdUser = appUserRepository.save(user);
return new ResponseEntity<>(createdUser,HtppStatus.CREATED);
}
Now next step TO MAKE THE URL OPEN for that we do below things:-
Now, to configure the URL we need to do spring Config in our project and for that
we go to our "config" package and create
In order to take the permission to open the Url we go to our "config" package and
create a class "SecurityConfig"
the purpose of adding this class is we are going to modify the configuration of
spring security
when I signUp the "username and password" will go to database and the username-
password is verify with the database
@Configuration //
public class SecurityConfig{
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws
Exception{
http.csrf().disable().cors().disable();
http.authorizeHttpRequests.anyRequest.permitAll();
return http.build();
}
here, we also make SecurityFilterChain method annoted with @Bean beacuse Spring IOC
have only limited information of object creation so when we get object from another
class than we use @Bean for that.
And, after that we write "return Http.build();" and the meaning of this line is
create an object with configuration that ultimately spring security is using it.
Now, The next step is "HOW TO ENCRYPT THE PASSWORD BEFORE I SAVE TO DATABASE"
The another way is:-/if no. of rounds of your encryption is 10 than the decryption
is also 10. So, higher the round you take for encrypt than the no. of round of
decryption is deteriote here.
public class B{
psvm(){
sout(BCrypt.hasPw("testing",BCrypt.genSalt(10)));
}
}
Before, You save this user what we do is, we go to user object and in user object
we set password like:-
user.setPassword(BCrypt.haspw(user.getPassword(),BCrypt.genSalt(10)));
if(appUserRepository.existsByUsername(user.getUsername()){
return new ResponseEntity<>("Username
exists",HttpStatus.Bad_Request);
}
And, from here we call userService and create a method verifyLogin in Service layer
and the return type is boolean.
public ResponseEntity<String> verifyLogin(@RequestBody Logindto loginDto){
boolean val = userService.verifyLogin(loginDto);
}
NOw, In order to search the record in our database on based of username we need to
create a method in repository layer which return of type Optional Class.
after that we write code in service layer for the search the record in database by
Username
from above code we call "userService.verifyLogin".