录:

1、默认表单认证
2、自定义表单登陆页
3、自定义数据库模型的认证和授权
    3.1、使用 mysql 创建数据库
    3.2、mybatis generator 生成代码
    3.3、springboot 整合 mybatis
    3.4、实现 UserDetails
    3.5、实现 UserDetailsService

 

1、默认表单认证   <--返回目录

  创建 springboot 项目,依赖:

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

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

  写一个测试 controller

@RestController
@RequestMapping("/index")
public class IndexController {
    @RequestMapping("/test1")
    public String test1(String name, Integer age) {
        return "test1";
    }
}

  启动项目,访问 http://localhost:8089/BootDemo/index/test1,弹出默认表单认证

Spring Security(一)自定义表单及认证授权(整合mybatis generator和mybatis)

 

   默认用户名为 user, 密码是动态生成并打印到控制台的一窜随机码。当然,用户名和密码可以在application.properties 中配置

spring.security.user.name=test
spring.security.user.password=123

 

2、自定义表单登陆页   <--返回目录

  虽然spring security 自带的表单登陆页可以方便快速地启动,但大多数应用程序更希望提供自己的的表单登陆页,此时就需要自定义表单登陆页。

Spring Security(一)自定义表单及认证授权(整合mybatis generator和mybatis)

   WebSecurityConfig

package com.oy;

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;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated()
            .and().formLogin().loginPage("/mylogin.html")
            .loginProcessingUrl("/login") // 指定处理登陆请求的路径
            .permitAll() // 登陆页和 "/login" 不设置权限
            .and().csrf().disable();
    }
}

  表单登陆页

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>自定义表单登陆页</h2>
<form action="login" method="post">
用户名:<input type="text" name="username" /><br/>&nbsp;&nbsp;&nbsp;码:<input type="text" name="password" /><br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
View Code

相关文章:

  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2021-04-10
  • 2021-11-19
  • 2022-12-23
  • 2021-08-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-06-22
  • 2021-07-10
  • 2021-05-13
相关资源
相似解决方案