一、錯誤代碼
1.首先我的結(jié)構(gòu)很簡單
首先實(shí)現(xiàn)登錄處理邏輯UserDetailServiceImpl 胁附,設(shè)置用戶名為admin ,固定密碼為123
`java`
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.根據(jù)用戶名去數(shù)據(jù)庫查詢,如果不存在拋出UsernameNotFoundException異常
if (!"admin".equals(username)){
throw? new UsernameNotFoundException("用戶名不存在");
}
//2.比較密碼(注冊時已經(jīng)加密過),若匹配成功返回UserDetails
//User類是UserDetailServiceImpl的實(shí)現(xiàn)類弹囚,其需要在表單中輸入的用戶名,密碼领曼,和權(quán)限
String password = passwordEncoder.encode("123");
return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));
}
}
在進(jìn)行Security配置 SecurityConfig 時鸥鹉,設(shè)置登錄成功跳轉(zhuǎn)頁面和登錄失敗跳轉(zhuǎn)頁面報錯
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login")
? ? ? ? ? ? ? ? //tologin和toError都做了跳轉(zhuǎn)
.loginProcessingUrl("/tologin")
//.defaultSuccessUrl("/main")
? ? ? ? ? ? ? ? .successForwardUrl("/main")
.failureForwardUrl("/toError");
? ? ? ? ? ? ? ? //.failureUrl("/error.html");
http.csrf().disable();
? ? ? ? //設(shè)置放行
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/error.html").permitAll()
.antMatchers("/toError").permitAll()
.anyRequest().authenticated();
}
@Bean
public PasswordEncoder getPw(){
return new BCryptPasswordEncoder();
}
}
二、錯誤展示
1.設(shè)置登錄成功跳轉(zhuǎn)頁面:
.loginProcessingUrl("/tologin")
2.在LoginControoler設(shè)置相應(yīng)的跳轉(zhuǎn)配置(后綴已經(jīng)配置為.html)
@RequestMapping("/login")
public String login()
{
return "login";
}
3.貼一個表單代碼
<form action="/tologin" method="post">
? ? ? ? <!--//本來在表單的用戶名和密碼的name屬性都是固定的username和password庶骄,但可以自定義入?yún)?/p>
.usernameParameter("username123")
.passwordParameter("password123")-->
? ? ? ? 用戶名:<input type="text" name="username123"/> <br/>
? ? ? ? 密碼:<input type="password" name="password123"/><br/>
? ? ? ? <input type="submit" value="登錄"/>
? ? </form>
到這里本來表單提交/tologin路徑毁渗,交給loginProcessingUrl中轉(zhuǎn)后判斷是否成功,若成功就進(jìn)入/toMain单刁,再進(jìn)控制器跳轉(zhuǎn)灸异。本應(yīng)該尋到main.html之后顯示登陸成功。
結(jié)果
并且報錯
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
大概意思就是不支持post請求羔飞。
我很疑惑肺樟,去查驗(yàn)資料
1.loginProcessingUrl? 與? successForwardUrl 使用post請求
2.registry.addViewController("/success").setViewName(“success”);這種路徑映射方式不支持post請求
3.重定向和轉(zhuǎn)發(fā)的區(qū)別:其中之一的區(qū)別是,重定向?yàn)g覽器地址欄會發(fā)生變化,為重定向路徑(/seccess),請求類型相應(yīng)的為/seccess的類型為get;轉(zhuǎn)發(fā)瀏覽器地址欄不會發(fā)生變化,還是登錄處理請求路徑(/doLogin),所以相應(yīng)的請求類型為post,所以就產(chǎn)生了異常逻淌。
并進(jìn)行了試錯
可知若想使用loginProcessingUrl? 與? successForwardUrl
1.則表單提交必須為post請求
2.不用addViewController的方式,而是寫一個controller,在控制器里進(jìn)行路徑映射
3.在映射時使用redirect:/xxx 么伯,進(jìn)行重定向,且直接指定到資源.html,或再次進(jìn)行映射
csdn鏈接:https://blog.csdn.net/qq_48826531/article/details/119278065