四登录验证:使用自定义界面登录

Posted 文nKkCJ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四登录验证:使用自定义界面登录相关的知识,希望对你有一定的参考价值。

2、自定义登录界面(thymeleaf)

1、html文件,放在resources/templates下为后面权限校验做准备,权限校验看五、的security配置类

1) login.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form action="/login" method="post">
    用户名 <input type="text" name="username"><br>
    密码 <input type= "password" name="password">
    <input type="submit" value="提交">
</form>

</body>
</html>

2)失败页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
失败的页面
<a href="/myLoginController">返回登录</a>
</body>
</html>

3)成功页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h3>首页</h3>
    <a href="/Vip1">拥有vip1 才能看的</a><br>
    <a href="/Vip2">拥有vip2 才能看的</a><br>
    <a href="/role">拥有abc 才能看的</a>
</div>
</body>
</html>

4)Vip1.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是 vip1 角色才能看见的信息
</body>
</html>

2、controller文件


@Controller
@RequestMapping()
public class UserController {
    
    // 登录页,跳转到/templates/login.html页面
    @RequestMapping("/login")
    public String login() {
        return "login";
    }
    // 首页,跳转到/templates/index.html页面
    @RequestMapping("/home")
    public String index() {
        return "home";
    }

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

3、 security配置


/**
 * SpringSecurity的配置
 * Created by macro on 2018/4/26.
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //请求权限配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login") // 放行userController中的login方法/login是对应方法名哦
                .loginProcessingUrl("/login") // 必须和表单提交 action 的名字 一样的,提交 username 和password
                // 设置登陆成功调用的接口,在springmvc中返回的值是string的话会先去找对应字符串名称的html,如果找不到就会抛出异常
                .successForwardUrl("/home")
                //重定向
                .successHandler(new AuthenticationSuccessHandler() {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.sendRedirect("https://www.baidu.com");
    }
})
        //失败跳转
        .failureForwardUrl("/toFail");
        http.authorizeRequests()
                .antMatchers("/login").permitAll()// 放行userController中的login方法/login是对应方法名哦
                .anyRequest().authenticated();
        http.csrf().disable(); //禁止csrf(跨域请求)
        @Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
//                .usernameParameter("username123")
//                .passwordParameter("password123")
                .loginPage("/myLoginController") // 一定要和 Controller 中 返回 myLogin页面 一致,
                .loginProcessingUrl("/login")// 必须和表单提交 action 的名字 一样的,提交 username 和password
                .successForwardUrl("/toSuccess");// 这个是 登录成功后返回的界面
//        .successHandler(new AuthenticationSuccessHandler() {
//            @Override
//            public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//                httpServletResponse.sendRedirect("https://www.baidu.com");
//            }
//        }).failureHandler(new ForwardAuthenticationFailureHandler("/toFail"));

        http.authorizeRequests()
                .antMatchers("/myLoginController").permitAll()// 放行myLoginController
                .antMatchers("/Vip1").hasAuthority("vip1")//数据看中存储的格式是vip1
                .antMatchers("/Vip2").hasAuthority("vip2")//数据看中存储的格式是vip2
                .antMatchers("/**/*.png").permitAll()
                .antMatchers("/toRole").hasRole("abc") //数据看中存储的格式是Role_abc
                .anyRequest().authenticated();
        http.csrf().disable();
    }


/*  可以不要哦,由于没有涉及到静态资源的调用,如用swagger接口文档这种可以在这里开启
    @Override
    public void configure(WebSecurity web) throws Exception {
        // 设置拦截忽略文件夹,可以对静态资源放行
        web.ignoring().antMatchers("/css/**", "/js/**");
    }*/
}

以上是关于四登录验证:使用自定义界面登录的主要内容,如果未能解决你的问题,请参考以下文章

使用 pgina 自定义 windows 登录界面

servlet登录界面进行用户名和密码验证

实验任务四-实现登陆界面的内容

PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码)

单点登录CAS使用记:实现自定义验证用户登录

FineReport中如何自定义登录界面