虽然Spring Security给我们提供了登录页面,但是对于实际项目中,大多喜欢使用自己的登录页面。所以Spring Security中不仅仅提供了登录页面,还支持用户自定义登录页面。实现过程也比较简单,只需要修改配置类即可。
别写登录页面,登录页面中
的action不编写对应控制器也可以。
内容 复制代码修改配置类中主要是设置哪个页面是登录页面。配置类需要继承WebSecurityConfigurerAdapter,并重写configure方法。
successForwardUrl()登录成功后跳转地址
loginPage() 登录页面
loginProcessingUrl 登录页面表单提交地址,此地址可以不真实存在。
antMatchers():匹配内容
permitAll():允许
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 表单认证 http.formLogin() .loginProcessingUrl("/login") //当发现/login时认为是登录,需要执行 UserDetailsServiceImpl .successForwardUrl("/toMain") //此处是post请求 .loginPage("/login.html"); // url 拦截 http.authorizeRequests() .antMatchers("/login.html").permitAll() //login.html不需要被认证 .anyRequest().authenticated();//所有的请求都必须被认证。必须登录后才能访问。 //关闭csrf防护 http.csrf().disable(); } @Bean public PasswordEncoder getPe(){ return new BCryptPasswordEncoder(); }}复制代码编写控制器,当用户登录成功后跳转toMain控制器。编写完成控制器后编写main.html。页面中随意写上一句话表示main.html页面内容即可。而之前的/login控制器方法是不执行的,所以可以删除了。
@Controllerpublic class LoginController {// 该方法不会被执行// @RequestMapping("/login")// public String login(){// System.out.println("执行了login方法");// return "redirect:main.html";// } @PostMapping("/toMain") public String toMain(){ return "redirect:/main.html"; }}复制代码表单处理中成功会跳转到一个地址,失败也可以跳转到一个地址中。
在src/main/resources/static下新建fail.html并编写如下内容
Title 操作失败,请重新登录. 跳转复制代码在配置方法中表单认证部分添加failureForwardUrl()方法,表示登录失败跳转的url。此处依然是POST请求,所以跳转到可以接收POST请求的控制器/fail中。
// 表单认证http.formLogin() .loginProcessingUrl("/login") //当发现/login时认为是登录,需要执行UserDetailsServiceImpl .successForwardUrl("/toMain") //此处是post请求 .failureForwardUrl("/fail") //登录失败跳转地址 .loginPage("/login.html");复制代码在控制器类中添加控制器方法,方法映射路径/fail。此处要注意:由于是POST请求访问/fail。所以如果返回值直接转发到fail.html中,及时有效果,控制台也会报警告,提示fail.html不支持POST访问方式。
@PostMapping("/fail")public String fail(){ return "redirect:/fail.html";}复制代码认证失败跳转到fail.html页面中,所以必须配置fail.html不需要被认证。需要修改配置类中内容
// url 拦截http.authorizeRequests() .antMatchers("/login.html").permitAll() //login.html不需要被认证 .antMatchers("/fail.html").permitAll() //fail.html不需要被认证 .anyRequest().authenticated();//所有的请求都必须被认证。必须登录后才能访问。复制代码当进行登录时会执行UsernamePasswordAuthenticationFilter过滤器。
usernamePasrameter:账户参数名
passwordParameter:密码参数名
postOnly=true:默认情况下只允许POST请求。
// 表单认证http.formLogin() .loginProcessingUrl("/login") //当发现/login时认为是登录,需要执行UserDetailsServiceImpl .successForwardUrl("/toMain") //此处是post请求 .failureForwardUrl("/fail") //登录失败跳转地址 .loginPage("/login.html") .usernameParameter("myusername") .passwordParameter("mypassword");复制代码 修改login.html
复制代码使用successForwardUrl()时表示成功后转发请求到地址。内部是通过successHandler()方法进行控制成功后交给哪个类进行处理
ForwardAuthenticationSuccessHandler内部就是最简单的请求转发。由于是请求转发,当遇到需要跳转到站外或在前后端分离的项目中就无法使用了。
当需要控制登录成功后去做一些事情时,可以进行自定义认证成功控制器。
新建类com.msb.handler.MyAuthenticationSuccessHandler编写如下:
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { //Principal 主体,存放了登录用户的信息 User user = (User)authentication.getPrincipal(); System.out.println(user.getUsername()); System.out.println(user.getPassword());//密码输出为null System.out.println(user.getAuthorities()); //重定向到百度。这只是一个示例,具体需要看项目业务需求 httpServletResponse.sendRedirect("http://www.baidu.com"); }}复制代码使用successHandler()方法设置成功后交给哪个对象进行处理
// 表单认证http.formLogin() .loginProcessingUrl("/login") //当发现/login时认为是登录,需要执行UserDetailsServiceImpl .successHandler(new MyAuthenticationSuccessHandler()) //.successForwardUrl("/toMain") //此处是post请求 .failureForwardUrl("/fail") //登录失败跳转地址 .loginPage("/login.html");复制代码ForwardAuthenticationFailureHandler中也是一个请求转发,并在request作用域中设置 SPRING_SECURITY_LAST_EXCEPTION的key,内容为异常对象。
新建com.msb.handler.MyForwardAuthenticationFailureHandler实现AuthenticationFailureHandler。在方法中添加重定向语句
public class MyForwardAuthenticationFailureHandler implements AuthenticationFailureHandler { @Override public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException { httpServletResponse.sendRedirect("/fail.html"); }}复制代码 修改配置类中表单登录部分。设置失败时交给失败处理器进行操作。failureForwardUrl和failureHandler不可共存。
// 表单认证 http.formLogin() .loginProcessingUrl("/login") //当发现/login时认为是登录,需要执行UserDetailsServiceImpl .successHandler(new MyAuthenticationSuccessHandler()) //.successForwardUrl("/toMain") //此处是post请求 .failureHandler(new MyForwardAuthenticationFailureHandler())// .failureForwardUrl("/fail") //登录失败跳转地址 .loginPage("/login.html"); | 留言与评论(共有 0 条评论) “” |