Spring Boot Security
Spring Boot Security
Authorization:it means whether user can access this functionality or resources in given system
according to their privileges or roles.(checking role and provide functionality according to their
roles and right.
it happen after user authentication.
Dependency:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
after adding dependency spring boot will provide default security with default login page having
username:user
password :auto generated in IDE console
Customized configuration:
spring.security.user.name=umang
spring.security.user.password=umang
Request goes to authentication filer (it is series of filter) .it find out which type of authentication
is present and create a object of that authentication filter .then this object send to authentication
manager(interface) and check authentication by authentication provider then this object stored
in security context
@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().antMatchers("/balance").authenticated().antMatchers("/
statement")
.authenticated().antMatchers("/myloan").authenticated().antMatchers("/home").permitAll()
.antMatchers("/contact").permitAll().and().httpBasic().and().formLogin();
return http.build();
}
}
http.formLogin();//whenever request comes from browser so request will be authenticated through default
login page ,login page will display.this authentication work when request come from browser.
http.httpBasic();// whenever request comes from postman so request will be authenticated by
passing the username and password in headers select authorization basic auth in postman.this
authentication works when request comes other than browser.
Director server will have the username password of the employee.when ever request comes from
company employee so it will check in directory server whether this request come from company
employees and valid request will move forward.
HOW MANY WAYS WE CAN CONFIGURE USER CREDENTIALS IN SPRING BOOT SECURITY
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
Now we can store the password without encoding
UserDetails user = User.builder()
.username("umang")
.password("umang")
.roles("USER")
.build();
JDBC AUTH
It is used when our api is globally used by user .So user credentials will be stored in database.
Client send request to server sending username and password.Server will authenticate the
credentials if request is valid server will generate jwt token and send toke to client in response.
Then client want to access secure resource it will send request to server with token.Server will
validate the token if it is valid send correct response to client otherwise send error response to
client.
Similar way oauth work(open authorization like we can login the website without
registering the website through third party app like gmail ,github and facebook)
Open authorization
Create a jwt token and sign with private key of rapipay in signature section of jwt token
And sent to a third party vendor and now the vendor will verify this token that it comes from
rapipay though his public key and it will give the bearer token to rapipay and now rapipay can
access other api through this bearer token. This bearer token has expiry time.