基于SpringSecurity+JWT登陆认证开发笔记一

基于SpringSecurity+JWT登陆认证开发笔记一

相关视频地址:https://www.bilibili.com/video/BV1kf4y1y78C?p=2

1建立新项目

初始配置
在这里插入图片描述
当我们创建好工程之后首先要做的是要写出基本的登录页面
新建一个控制器 HomeController

package com.example.login.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController  {
    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/system/user")
    public String userList(){
        return "user";
    }
    @GetMapping("/system/role")
    public String roleList(){
        return "role";
    }
    @GetMapping("/system/menu")
    public String menuList(){
        return "menu";
    }
    @GetMapping("/system/order")
    public String orederList(){
        return "order";
    }

}

再resources/template目录下创建出对应的文件
index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body><head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>
    用户登录
</h1>
<br>
<a href="/system/user">
    用户管理
</a>
<br>
<a href="/system/menu">
    目录管理
</a>
<br>
<a href="/system/role">
    角色管理
</a>
<br>
<a href="/system/order">
    订单管理
</a>
<a href="/logout">
    退出
</a>
</body>
</html>

仅仅是做个简单的页面又来测试
其他的html文件都差不多以menu.html为例

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
</head>
<body>
<h1>目录页面</h1>
</body>
</html>

简单写写能够区分出是那个页面即可
接下来需要我们自定义出一个login页面
login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <script src="https://cdn.staticfile.org/jquery/1.12.3/jquery.min.js"></script>
</head>
<body>
<h1>实训管理系统</h1>
<form action="/login" method="post">
    <span>用户账号</span><input type="txt" name="username" id="userid"/><br>
    <span>用户密码</span><input type="password" name="passwd" id="userpasswd"/><br>
    <input type="submit" value="登录">
</form>

</body>
</html>

接下来对于页面进行配置
新建目录config.security
在该目录下建立SecurityConfig文件

package com.example.login.config.security;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity httpSecurity)throws Exception{
        httpSecurity.formLogin()
                .loginPage("/login.html")//确定自定义的登录页
                .loginProcessingUrl("/login")//确定访问时的地址
                .usernameParameter("username")//与login上传的username和password相对应
                .passwordParameter("password")
                .defaultSuccessUrl("/index")//认证成功之后进入的页面
                .failureUrl("/login.html")//失败的页面
                .and()
                .authorizeRequests()//对于权限的配置
                .antMatchers("/login.html","/login")
                .permitAll()//login页面对于所有角色开放
                .antMatchers("/order")
                .hasAnyAuthority("ROLE_user","ROLE_admin")//对于admin与user开放
                .antMatchers("/system/user","system/role","system/menu")
                .hasAnyRole("admin")//对admin开放
                .anyRequest().authenticated()
                .and()
                .csrf().disable();

        httpSecurity.logout().logoutUrl("/logout");//退出页面

    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth)throws Exception{//未连接数据库,进行内存认证
        auth.inMemoryAuthentication()
                .withUser("user")
                .password(bCryptPasswordEncoder().encode("123456"))
                .roles("user")
                .and()
                .withUser("admin")
                .password(bCryptPasswordEncoder().encode("123456"))
                .roles("admin")
                .and()
                .passwordEncoder(bCryptPasswordEncoder());
    }
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

}

在这里插入图片描述
在这里插入图片描述
页面可以说是十分简陋,不过基础的登录功能已经实现了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值