学习SpringSecurity的日常生活(二)——自定义登录界面


前言

`上一篇我们已经配置了SpringSecurity的接口路径的权限,但是我们说到官方的登录界面太简单而且不好扩展,基本上只是为了展示功能用的,所以这次我们要定义登录界面。

1、创建一个登录的html

我们这里方便前端页面的访问显示,这里使用thymeleaf,引入jar包

	<dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

将配置文件里面加上

spring.thymeleaf.cache=false

让他没有缓存

接着我们在Template目录下面创建一个login.html,以及在后端创建一个LoginController。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录界面</title>
</head>
<body>
<form method="post" action="/dologin">
    用户名:<input name="username" type="text"/><br>
    密码: <input name="password" type="text">
    <input type="submit" name="登录">
</form>
</body>
</html>
@Controller
public class LoginController {

    @RequestMapping("/tologin")
    public String login(){
        return "login";
    }
}

新建一个index.html方便验证测试,同时在controller里面添加index的接口

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主界面</title>
</head>
<body>
登陆成功
</body>
</html>
@RequestMapping("/index")
public String index(){
    return "this is index";
}

2、修改配置

接着我们修改SecurityConfig里面的配置,使用formLogin()表单提交,修改登录的页面loginPage(“/tologin”),当然我们要把tologin方法释放,不然如果tologin接口还需要验证就没有意义了。
csrf()是用来防止跨站攻击的,我们暂时先设置为disable()

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/hello").permitAll()
                .antMatchers("/tologin").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/tologin")
                .and()
                .csrf().disable();
    }

我们启动项目访问浏览器,可以发现跳到了我们自己的登录页面
在这里插入图片描述
那么现在登录页面有了,我们如何才能让这个登录页面实现有效的登录操作呢?

修改配置文件,增加loginProcessingUrl(“/dologin”),这里的路径和form表单里面的action一致,是用来验证登录的路径

注意,form表单里面的请求方式一定为post,用户名的name默认为username,密码的name默认为password,提交按钮类型为submit。如果要修改可以用usernameParameter("/uname") passwordParameter("passwd")

我们重启项目,访问浏览器 http://localhost:8080/index,我们可以发现成功了。
在这里插入图片描述
这里我们登陆成功直接就访问了接口资源,但是在我们项目里一般登陆成功或者失败的话都会跳到某个具体的页面,如果是前后端分离的话,会返回具体的json到前端。

我们创建index.html以及fail.html来测试登陆之后的跳转。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主界面</title>
</head>
<body>
登陆成功
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>错误界面</title>
</head>
<body>
登陆失败
</body>
</html>

以及对应的接口

@RequestMapping("/fail")
public String fail(){
     return "fail";
 }
 @RequestMapping("/toindex")
 public String toindex(){
     return "index";
 }

我们修改securityconfig配置类,增加了验证成功的跳转defaultSuccessUrl以及验证失败的跳转。

这里需要注意的是defaultSuccessUrl,如果后面的参数为false则跳转到验证之前的那一次请求结果,如果为true就跳转到toindex,默认不填为false

defaultSuccessUrl和failureUrl这两个都是redirect请求,如果想要forward请求,可以使用successForwardUrl以及failureForwardUrl。

http.authorizeRequests()
                .antMatchers("/hello").permitAll()
                .antMatchers("/tologin").permitAll()
                 .antMatchers("/fail").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/tologin")
                .loginProcessingUrl("/dologin")
                .defaultSuccessUrl("/toindex",true)
                .failureUrl("/fail")
                .and()
                .csrf().disable();

除此之外,我们还可以不使用url跳转,可以使用自定义处理器跳转

public class SuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Map<String,Object> map = new HashMap<>();
        map.put("code",HttpStatus.OK.value());
        map.put("msg","登陆成功");
        map.put("authentication",authentication);
        String s = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(s);
    }
}
public class FailHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        Map<String,Object> map = new HashMap<>();
        map.put("code",HttpStatus.UNAUTHORIZED.value());
        map.put("msg","登陆失败");
        String s = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(s);
    }
}
http.authorizeRequests()
                .antMatchers("/hello").permitAll()
                .antMatchers("/tologin").permitAll()
                 .antMatchers("/fail").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/tologin")
                .loginProcessingUrl("/dologin")
//                .defaultSuccessUrl("/toindex",true)
//                .failureUrl("/fail")
                 .successHandler(new SuccessHandler())
                 .failureHandler(new FailHandler())
                .and()
                .csrf().disable();

我们重启项目,访问浏览器输入正确的用户名密码以及错误的用户名密码就会看到处理器输出的内容。

结束语

这里我们的自定义表单登录就可以了,不过这是前后端不分离的,如果前后端分离我们应该怎么自定义登录呢,下一篇我们一起讨论一下。

项目下载:项目下载地址
提取码:xxgz

我的个人博客地址http://www.dbhx.vip

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值