Spring Boot: How to specify the PasswordEncoder?

前端 未结 15 1180
醉梦人生
醉梦人生 2020-12-02 06:27

Currently I got the main class:

package com.recweb.springboot;

import org.springframework.boot.SpringApplication;
im         


        
15条回答
  •  情书的邮戳
    2020-12-02 06:53

    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;
    
        }
    }
        
    

提交回复
热议问题