springboot使用springsecurity认证授权

官网:https://spring.io/projects/spring-security

一:建立并配置项目

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
导入thymeleaf依赖:

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

在这里插入图片描述导入security依赖:

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

在这里插入图片描述然后建立对应的html文件结构。
在这里插入图片描述建立config对应文件,路由可以用自配的,也可以直接去Controller中写,这里主要介绍Security的配置文件。

在这里插入图片描述

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

路由详情:
在这里插入图片描述

@Configuration
public class MyMvcConfig  implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/vip1").setViewName("/vip1/vip1");
            registry.addViewController("/vip2").setViewName("/vip2/vip2");
    }
}

yaml中关闭thymeleaf缓存:

spring:
  thymeleaf:
    cache: false

server:
  port: 8045

访问,出现如下界面说明security已经拦截了我们的请求
在这里插入图片描述

二:配置Security规则


于是现在我们希望它不再拦截我们的所有页面,放开login页,但其他页面需要权限访问,在Security文件中修改:

.antMatchers("/"):添加一个security的路由路径
.permitAll():允许所有人访问
.hasRole(“vip2”):设定特定人访问

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/vip1").hasRole("vip1")
                .antMatchers("/vip2").hasRole("vip2");

        //权限不足跳转登录页面
        http.formLogin();

    }
}

访问的时候主页随便进,但访问其他路径会提示权限不允许
在这里插入图片描述在这里插入图片描述但是这样还是不能登录,所以需要进行配置密码和用户。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/vip1").hasRole("vip1")
                .antMatchers("/vip2").hasRole("vip2");

        //权限不足跳转登录页面
        http.formLogin();

		//注销,直接在前台用thymeleaf加th:href="@{/logout}"即可
        http.logout().logoutSuccessUrl("/");
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
              .withUser("admin").password(new BCryptPasswordEncoder().encode("123123")).roles("vip1","vip2")
              .and()
              .withUser("user").password(new BCryptPasswordEncoder().encode("123123")).roles("vip1");
    }
}

然后输入密码,对应的角色即可访问我们的页面

在这里插入图片描述

以及可以配置一下rememberme功能。
在这里插入图片描述在这里插入图片描述
当然如果不想要它默认提供的页面,可以用loginpage指向自己定制的页面

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deeeelete

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值