springsecurity

SpringSecurity和Shiro

认证、授权

SpringSecurity(安全)

过滤器和拦截器可以实现安全

简介

pring Security 是一个功能强大且高度可定制的身份验证和访问控制框架。它是保护基于 Spring 的应用程序的事实标准。

Spring Security 是一个专注于为 Java 应用程序提供身份验证授权的框架。像所有 Spring 项目一样,Spring Security 的真正强大之处在于它可以轻松扩展以满足自定义需求。

关键类:
WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
@EnableWebSecurity:开启WebSecurity模式

案例

1.导入pom依赖

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

2.继承WebSecurityConfigurerAdapter重写configure方法

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        // 首页所有人可以访问,功能页只有对应有权限的人才能访问
        // permitAll允许所有人登录的地址
        // hasRole需要权限的登录
        http.authorizeHttpRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        // 没有权限默认会到登录页,开启登录页面
        // loginPage(url) 登录成功后的请求url
        // loginProcessingUrl 真实处理登录的路径
        // usernameParameter 登录的name
        // passwordParameter 密码的name
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login").usernameParameter("username").passwordParameter("password");

        // 你开启登出功能
//        http.logout();

        //注销,开启注销功能,跳到首页
//        http.logout().deleteCookies("remove").invalidateHttpSession(true);

        // 注销成功去首页,注销成功后跳转页面
        http.logout().logoutSuccessUrl("/toLogin");

        // 关闭跨站csrf攻击检测
        http.csrf().disable();

        // 开启记住我    记住我的单选框的name是remeber
        http.rememberMe().rememberMeParameter("remember");

    }

    // 认证
    // springsecurity 5+,新增加密方式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        // jdbcAuthentication通过数据库的认证
        // auth.jdbcAuthentication().withUser()

        // 内存中的认证
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("admin")
                .password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123")).roles("vip3");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值