Currently I got the main class:
package com.recweb.springboot;
import org.springframework.boot.SpringApplication;
im
According to spring security 5.0 's new feature. They write this.
Spring Security’s PasswordEncoder interface is used to perform a one way transformation of a password to allow the password to be stored securely. Given PasswordEncoder is a one way transformation, it is not intended when the password transformation needs to be two way (i.e. storing credentials used to authenticate to a database). Typically PasswordEncoder is used for storing a password that needs to be compared to a user provided password at the time of authentication.
So i tried this Mutiple HttpSecurity. This s my security configuration. Hope it help you.
@Configuration
@EnableWebSecurity
public class SecurityConfig
{
private final EdminService edminService;
public SecurityConfig ( final EdminService edminService ){
this.edminService=edminService;
}
@Bean
public UserDetailsService userDetailsService() throw Exception {
UserBuilder users= Users.withDefaultPasswordEncoder;
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
List<EdminEntity> edminList=this.edminService.findAll();
for(EdminEntity edmin: edminList){
manager.createUser(users.username(edmin.getEdminname())
.password(edmin.getEdminrpass()).roles("EDMIN_ROLE").build());
}
return manager;
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home","/vendor/**","/image/**","/home/**").permitAll()
.antMatchers("/admin/**").hasRole("EDMIN_ROLE")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/home")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));}
}
}
Sorry for my english and thanks for read my answer.
Try this in SecurityConfig extends WebSecurityConfigurerAdapter
:
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception{
auth
.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.and()
.withUser("admin")
.password(passwordEncoder().encode("admin"))
.roles("USER", "ADMIN");
}
Work for me
Had IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
exception in my initial app loading startup housekeeper. (Just needed to normalize data in DB before the app start.) With this (manually set an authenticated user) approach you can create UsernamePasswordAuthenticationToken
and setAuthentication. You can get IllegalArgumentException, if you don't specify 3-rd argument AuthorityList
.
UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken(
admin.getEmail(),
UserServiceImpl.PASSWORD_ENCODER.encode(admin.getPassword()),
AuthorityUtils.createAuthorityList(admin.getRoles()) // <= had to add this line to resolve the exception
);
SecurityContextHolder.getContext().setAuthentication(authReq);
Maybe some other setup steps could also work, but setting AuthorityList is good enough for me. And you can at least dig in this direction if can't find a solution on this page.
If you are doing in Memory Authentication, here's the code on how to go about it. Tested on Spring Boot 2.3.3.RELEASE
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
static PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("testuser").password(encoder.encode("testpassword")).roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers("/sites.xhtml/**").hasRole("ADMIN").anyRequest().fullyAuthenticated().and()
.httpBasic();
}
@Bean
public static PasswordEncoder passwordEncoder() {
return encoder;
}
}
In spring-security-core:5.0.0.RC1
, the default PasswordEncoder
is built as a DelegatingPasswordEncoder
. When you store the users in memory, you are providing the passwords in plain text and when trying to retrieve the encoder from the DelegatingPasswordEncoder
to validate the password it can't find one that matches the way in which these passwords were stored.
Use this way to create users instead.
User.withDefaultPasswordEncoder().username("user").password("user").roles("USER").build();
You can also simply prefix {noop}
to your passwords in order for the DelegatingPasswordEncoder
use the NoOpPasswordEncoder
to validate these passwords. Notice that NoOpPasswordEncoder
is deprecated though, as it is not a good practice to store passwords in plain text.
User.withUsername("user").password("{noop}user").roles("USER").build();
For more information, check this post.
https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding
Use NoOpPasswordEncoder
for inMemoryAuthentication
auth.inMemoryAuthentication()
.withUser("user")
.password("{noop}password")
.roles("USER")