SpringBoot整合Spring Security,使用formLogin模式进行鉴权(二)

简介: 在configure(HttpSecurity http)方法里写入如下代码

一、使用formLogin模式进行鉴权

在configure(HttpSecurity http)方法里写入如下代码

http.csrf().disable()
                .formLogin()
                .loginPage("/login.html")//设置登录页,该页面放在public目录下
                .loginProcessingUrl("/login")//设置登录请求地址,默认就是login
                .defaultSuccessUrl("/index")//设置登录成功后跳转地址
                .usernameParameter("uname")//配置用户名参数,默认是username
                .passwordParameter("pword")//配置密码参数,默认是password
                .and()
                .authorizeRequests()
                .antMatchers("/login.html","/login").permitAll()//配置无需认证即可访问的请求
                .antMatchers("/biz1","/biz2").hasAnyAuthority("ROLE_user","ROLE_admin")//配置需要权限才能访问的请求
                .antMatchers("/syslog","/sysuser").hasAnyAuthority("ROLE_admin")
                .anyRequest().authenticated();//配置其他的所有请求需要认证


然后重写另外两个configure方法

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //从内存里获取用户数据
        auth.inMemoryAuthentication()
                //配置用户信息
                .withUser("user").authorities("ROLE_user").password(new BCryptPasswordEncoder().encode("1"))
                .and()
                //配置管理员信息
                .withUser("admin").authorities("ROLE_user","ROLE_admin").password(new BCryptPasswordEncoder().encode("1"))
                .and()
                //密码均采用BCrypt加密方式
                .passwordEncoder(new BCryptPasswordEncoder());
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        //放行所有静态资源
        web.ignoring()
                .antMatchers("/css/**","/font/**","/js/**","/img/**");
    }


自定义表单代码:

<form action="/login" method="post">
    <span>用户名称</span><input type="text" name="uname" id="username"/> <br>
    <span>用户密码</span><input type="password" name="pword" id="password"/> <br>
    <span>验证码</span><input type="text" name="captchaCode" id="captchaCode"/>
    <input type="submit" value="登陆">
    <label><input type="checkbox" name="remember-me" id="remember-me"/>记住密码</label>
</form>


记住我功能在configure(HttpSecurity http)方法里加入:

        //开启记住我功能,默认参数是remember-me
        http.rememberMe();


勾选记住我登录成功后,就会有一个remember-me的cookie20200921232816761.png

目录
相关文章
|
2月前
|
安全 Java 数据安全/隐私保护
使用Spring Security实现细粒度的权限控制
使用Spring Security实现细粒度的权限控制
|
24天前
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
127 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
12天前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
2月前
|
监控 Java 应用服务中间件
什么是 Spring Boot,及为什么要用 Spring Boot
**Spring Boot**: 2013年起研,简化Spring笨重配置,集成常用库,开箱即用,少代码配置,专注业务。 **为何选Spring Boot?** 出色基因,快速搭建;单一依赖替多;Java Config简化配置;内嵌Tomcat,简化部署;监控REST化;微服务友好,趋势之选。
85 27
|
16天前
|
NoSQL 关系型数据库 MySQL
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
52 2
|
16天前
|
安全 Java 数据安全/隐私保护
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
本文介绍了一个基于SpringBoot、Spring Security和JPA开发的校园图书管理系统,包括系统的核心控制器`LoginController`的代码实现,该控制器处理用户登录、注销、密码更新、角色管理等功能,并提供了系统初始化测试数据的方法。
23 0
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
|
18天前
|
安全 Java 数据库
|
18天前
|
JSON 安全 Java
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
67 11
|
6天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
下一篇
云函数