当前位置 博文首页 > m0_46212244的博客:springboot整合springSecurity使用

    m0_46212244的博客:springboot整合springSecurity使用

    作者:[db:作者] 时间:2021-09-23 13:25

    1.导入依赖

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

    2.代码演示

    package com.zte.mds.web.config.security;
    
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
    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 httpSecurity) throws Exception {
            httpSecurity.authorizeRequests()
                    // 表示主页都可以访问
                    .antMatchers("/").permitAll()
                    // 表示只有vip可以访问
                    .antMatchers("/level1/**").hasRole("vip")
                    // 表示只有超级vip可以访问
                    .antMatchers("/level2/**").hasRole("superVip")
                    // 表示只有plus超级vip可以访问
                    .antMatchers("/level3/**").hasRole("superVipPlus");
    
            // 如果没有权限默认前往登录页面
            httpSecurity.formLogin()
                    // 这个设置为你前端页面的提交表达的命名
                    .usernameParameter("userName")
                    // 同理
                    .passwordParameter("passWord")
                    // 跳转到定制页
                    .loginPage("/login")
                    // 登陆表单提交请求
                    .loginProcessingUrl("/toLogin");
    
            // 防止网站工具:get post
            httpSecurity.csrf().disable();  // 关闭csrf功能 防止注销失败的情况
    
            // 注销  --默认返回登录页面 可以使用logoutSuccessUrl跳到指定地址
            httpSecurity.logout().logoutSuccessUrl("/");
    
            // 开启记住我功能  --定制记住我的参数
            httpSecurity.rememberMe().rememberMeParameter("remember");
        }
    
        // 认证
    
        /**
         * Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
         * 我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
         * spring security 官方推荐的是使用bcrypt加密方式。
         * @param auth
         * @throws Exception
         */
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            // 密码加密
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("1437").password(new BCryptPasswordEncoder().encode("201437")).roles("vip", "superVip", "superVipPlus")
                    .and()
                    .withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip", "superVip", "superVipPlus")
                    .and()
                    .withUser("201437").password(new BCryptPasswordEncoder().encode("123456")).roles("vip", "superVip");
        }
    }
    
    

    PS:只是一个参考,可以做个了解

    cs
    下一篇:没有了