使用springboot,权限管理使用spring security,使用内存用户验证,但无响应报错:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
解决方法:
spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例,否则页面毫无响应。
PasswordEncorder的实现类。
package com.example.demo.security;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
*
* spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例
* @author lf
* @date 2019/6/6 - 17:31
*/
public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence charSequence) {
return charSequence.toString();
}
@Override
public boolean matches(CharSequence charSequence, String s) {
return s.equals(charSequence.toString());
}
}
然后修改:Security配置类
package com.example.demo.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @author lf
* @date 2019/6/6 - 13:49
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()//启用默认登陆界面
.failureForwardUrl("/login?error")
//登录失败返回URL:/login?error
.defaultSuccessUrl("/ayUser/test ")
//登录成功跳转URL:这里跳转到用户的首页
.permitAll();//登录页面全部权限可访问
super.configure(http);
/* //设置登录,注销,表单登录不用拦截,其他请求要拦截
http.authorizeRequests().antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
//关闭默认的csrf认证
http.csrf().disable();*/
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// super.configure(auth);
auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
.withUser("阿毅").password("123456").roles("ADMIN")
.and()
.withUser("阿兰").password("123456").roles("USER");
}
@Override
public void configure(WebSecurity web) throws Exception {
// super.configure(web);
//设置静态资源不要拦截
web.ignoring().antMatchers("/js/**", "/cs/**", "/images/**");
}
}