Springboot Part:10

Опубликовано: 08 Июль 2026
на канале: GIRISH KUMAR MISHRA
124
2

Way to generate bcrypt password:
public static void main(String[] args) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
encoded = encoder.encode("Best@123");//Girish's password
System.out.println("Girish :"+encoded);
SpringApplication.run(Demo1Application.class, args);
}
OR
https://bcrypt-generator.com/
Table creation:
---------------------------------------------------------------------------------------------------------
DROP TABLE authorities;
DROP TABLE users;

create table users (
username varchar(50) not null primary key,
password varchar(120) not null,
enabled tinyint(1) not null
);

create table authorities (
username varchar(50) not null,
authority varchar(50) not null,
foreign key (username) references users (username)
);

insert into users(username, password, enabled)values('girish','$2y$10$sBiPavfQWXNBy9MICuO6Xuf.fasWvpKBOdjXUgMZ8ro8hEhvQTYaq',1);
insert into authorities(username,authority)values('girish','ROLE_ADMIN');

insert into users(username, password, enabled)values('rajeev','$2y$10$URffSmEyBZpQ2HqECC32KeKIBDGQn2e7VgSINa2STRhW2nrHvGzMu',1);
insert into authorities(username,authority)values('rajeev','ROLE_USER');
-----------------------------------------------------------------------------------------------------------------
Java Security configuration details:
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public JdbcUserDetailsManager jdbcUserDetailsManager() {
return new JdbcUserDetailsManager(dataSource);
}
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception{
httpSecurity.authorizeRequests()
.antMatchers("/rest/getEmploees").hasRole("USER")
.anyRequest()
.authenticated()
.and()
.formLogin();
}
---------------------------------------------------------------------------------------------------