原代碼為:
protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("rg2") .password("123456") .roles("ADMIN"); }
記過發現報錯Spring Security 報There is no PasswordEncoder mapped for the id "null"
原因是Spring Security 升級到5版本后密碼支持多種加密格式;
添加一個新的類
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()); } }
然后再原代碼中改為
protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()) .withUser("rg2") .password("123456") .roles("ADMIN"); }