zoukankan      html  css  js  c++  java
  • SpringSecurity常见用法备忘

    package com.botao.securitydemo1.config;
    
    import com.botao.securitydemo1.Service.UserService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    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.builders.WebSecurity;
    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;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Bean
        public BCryptPasswordEncoder bCryptPasswordEncoder(){
            return new BCryptPasswordEncoder();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            /**
             * 请求授权的规则
             */
            http.authorizeRequests()
                    .antMatchers("/").permitAll()
                    .antMatchers("/a/**").hasRole("vip1")
                    .antMatchers("/b/**").hasRole("vip2")
                    .antMatchers("/c/**").hasRole("vip3").anyRequest().authenticated();
            http.formLogin();
    
            //登录
            //http.formLogin().loginPage("/login").loginProcessingUrl("/check").usernameParameter("username").passwordParameter("psw");
            //退出
            //http.logout().logoutUrl("/logout").logoutSuccessUrl("/").permitAll();
            //记住我
            //http.rememberMe().rememberMeParameter("remember");
            //关闭csrf
            http.csrf().disable();
    
            //没有权限就进xxx.html(403)
            //http.exceptionHandling().accessDeniedPage("/xxx.html");
        }
    
    
        //认证
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("a").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1")
                    .and()
                    .withUser("b").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2")
                    .and()
                    .withUser("c").password(new BCryptPasswordEncoder().encode("123456")).roles("ivp3")
                    .and()
                    .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3");
    
        }
    }
  • 相关阅读:
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    使用 acl_cpp 的 HttpServlet 类及服务器框架编写WEB服务器程序(系列文章)
    C编译器剖析PDF文档及UCC编译器162.3
    深入浅出NodeJS——数据通信,NET模块运行机制
    看AngularJS
    如何编写高效的jQuery代码
    Express安装入门与模版引擎ejs
    使用SeaJS实现模块化JavaScript开发
    RequireJS 入门指南
    RequireJS
  • 原文地址:https://www.cnblogs.com/botaoJava/p/13866954.html
Copyright © 2011-2022 走看看