开发实例三(登录进行读取数据库表数据)

知识梳理与开发步骤 同时被 3 个专栏收录
4 篇文章 0 订阅
1 篇文章 0 订阅
1 篇文章 0 订阅

第一步、创建系统用户实体类:

@Data
public class SysUser {

private Long id;
private String userName;
private String passWord;
private Date createTime;
private Integer userAge;
private String remark;

}

第二步、创建系统用户持久层;

@Mapper
public interface SysUserDao extends BaseMapper<SysUser> {
}

第三步

public interface SysUserService {
}

@Service("SysUserService")
public class SysUserServiceImp extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {

}

第四步、创建系统用户控制层;

@RestController
public class SysUserController {

    @Autowired
    SysUserServiceImp sysUserServiceImp;

    @GetMapping("/getUser")
    public SysUser getUser(Long id){
        return sysUserServiceImp.getById(id);
    }

    @PostMapping("/insertUser")
    public String insertUser(SysUser sysUser){
        sysUser.setCreateTime(new Date());
        return sysUserServiceImp.save(sysUser)?"保存成功":"保存失败";
    }

    @PutMapping("/updateUser")
    public String updateUser(SysUser sysUser){
        return sysUserServiceImp.updateById(sysUser)?"修改成功":"修改失败";
    }

    @GetMapping("/getList")
    public List<SysUser> getList(){
        return sysUserServiceImp.list();
    }
}

任务6.6:添加权限框架Spring Security

任务描述
(1)添加相关依赖;
(2)简单模式测试;
(3)添加配置测试;
(4)添加内存账号测试;
(5)添加自定义密码编解码测试;

任务实施

第一步、在项目pom.xml文件中添加依赖:

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

第二步、简单模式测试,直接访问http://localhost:8080/

1

第三步、添加配置测试,直接访问http://localhost:8080/:

(1)创建配置类:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
    }
}
(2)添加接口方法:
@GetMapping("/")
public String index(Long id){
    return "欢迎使用!";
}


第四步:添加内存账号测试

1)增加内存账号配置到配置类:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
}

```@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
}

## (2)访http://localhost:8080/getList ,输入账号admin,密码123456:

![2](https://img-blog.csdnimg.cn/20200904215619847.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc2NjkwOA==,size_16,color_FFFFFF,t_70#pic_center)
![3](https://img-blog.csdnimg.cn/2020090421563015.png#pic_center)
## 第五步、添加自定义密码编解码测试;
(1)创建自定义密码编解码类:

在这里插入代码片

public class MyPasswordEncoder implements PasswordEncoder {

    final static String ENCODER_PWD = "123456";
	
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword + ENCODER_PWD;
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return encodedPassword.equals(rawPassword+ENCODER_PWD);
    }
}
## (2)修改配置用户密码编解码方式:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser(“admin”).password(new MyPasswordEncoder().encode(“123456”)).roles(“ADMIN”);
}

## (3)访http://localhost:8080/getList ,输入账号admin,密码123456:
![5](https://img-blog.csdnimg.cn/2020090421580399.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDc2NjkwOA==,size_16,color_FFFFFF,t_70#pic_center)
![7](https://img-blog.csdnimg.cn/20200904215812520.png#pic_center)

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

⺌北城南笙。

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值