学习在spring boot中使用spring-security(二)

目录

 前置知识

自定义登陆页面

 先说自定义jsp页面

自定义使用vue页面

连接数据库查询用户名密码

连接数据库查询数据

配置


我的信息比较落后,今天才知道一件事,以下出自官网说明。要不要再学习看下去,自己做抉择。

 大家自行理解什么意思。

The Spring Security OAuth project is deprecated. The latest OAuth 2.0 support is provided by Spring Security. See the OAuth 2.0 Migration Guide for further details.

大概意思就是说,它不再维护了

补充: 2020/04/15 这天,Spring 团队又宣布新起《Spring Authorization Server》项目,以继续提供对 Spring 授权服务器的支持。

 前置知识

学习在spring boot中使用spring-security(一)_wai_58934的博客-CSDN博客spring boot整合spring-security简单入门案例,包看包会!https://blog.csdn.net/wai_58934/article/details/122350502?spm=1001.2014.3001.5501

自定义登陆页面

 先说自定义jsp页面

引入thymeleaf,为了方便访问jsp

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

配置路径前缀和后缀,因为我把jsp页面放在了resources下的templates下

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.jsp

自定义一个跳转到jsp的controller,可以把之前写的WebViewConfig给注释掉了

@Controller
public class PageController {
    @RequestMapping("/loginx")
    public String login(){
        return "loginx";
    }
}

给一个随意的jsp登陆页面

<html>
<head>
    <title>login.jsp页面</title>
</head>
<body>
//这个action要和WebSecurityConfig类里重写的configure下的loginProcessingUrl链式方法配合,下讲
<form action="login" method="post" name=form>
    <font size="5">登录界面</font><br>
    用户名:<input type="text" value="" name="username"><br>
    密 码:<input type="text" value="" name="password"><br>
    <input type="submit" value="提交" name="submit">
</form>
</body>
</html>

配置WebSecurityConfig。和前置知识博客一配置基本相同,只展示重写的configure配置

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable() //新增,关闭csrf验证,这个需要暂时关闭功能才能正常使用
                .authorizeRequests()
                .antMatchers("/q/q1").hasAuthority("a")
                .antMatchers("/q/q2").hasAuthority("b")
                .antMatchers("/q/**").authenticated()
                .anyRequest().permitAll()
                .and()
                .formLogin()
                .loginPage("/loginx")//新增,配置对应的页面,也就是我们写的jsp
                .loginProcessingUrl("/login")//新增,配置提交时对应的action
                .successForwardUrl("/loginsuccess");
    }

以上表述已经很详细了,很简单了,需要改的东西不多。

自定义使用vue页面

今天有点晚了,下篇再写

连接数据库查询用户名密码

之前(一)的时候我们把用户设置到了内存中,平时开发我们都会去查询数据库

连接数据库查询数据

这个就不用说了吧,不会的移步这里

mybatis-plus保姆级别初级使用_wai_58934的博客-CSDN博客包 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <depenhttps://blog.csdn.net/wai_58934/article/details/121733644?spm=1001.2014.3001.5501

配置

我们只需要继承UserDetailsService,然后交给spring容器就可以了。记得把之前写在内存中的人删了。

@Component
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    UserService userService;
    @Override
    //这里的参数username就是你登陆时传过来的账号
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {       
        //从数据库根据名字查询
        UserDao user = userService.getByName(username);
        //如果没有这个人则返回空
        if (user==null){
            return null;
        }
        //这里只说账号密码认证,暂时不说授权,所以静态写为p1
        UserDetails userDetails = User.withUsername(user.getUsername()).password(user.getpassword()).authorities("p1").build();
        return userDetails;
    }
}

可以自行测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值