• [Spring Security] An Simple example configuration


    package com.example.ec.security;
    
    import com.example.ec.repo.RoleRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.config.http.SessionCreationPolicy;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Autowired
        RoleRepository roleRepository;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            // Entry points
            http.authorizeRequests()
                    .antMatchers("/packages/**").permitAll()
                    .antMatchers("/tours/**").permitAll()
                    .antMatchers("/ratings/**").permitAll()
                    .antMatchers("/users/signin").permitAll()
                    // Disallow everything else..
                    .anyRequest().authenticated();
    
            // Disable CSRF (cross site request forgery)
            http.csrf().disable();
    
            // No session will be created or used by spring security
            http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    
        }
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder(12);
        }
    
    }
    @Component
    public class ExploreCaliUserDetailsService implements UserDetailsService {
        @Autowired
        private UserRepository userRepository;
    
        @Override
        public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
            User user = userRepository.findByUsername(s).orElseThrow(() ->
                    new UsernameNotFoundException(String.format("User with name %s does not exist", s)));
    
            //org.springframework.security.core.userdetails.User.withUsername() builder
            return withUsername(user.getUsername())
                    .password(user.getPassword())
                    .authorities(user.getRoles())
                    .accountExpired(false)
                    .accountLocked(false)
                    .credentialsExpired(false)
                    .disabled(false)
                    .build();
        }
    }
  • 相关阅读:
    Spring Boot面试杀手锏————自动配置原理
    session在什么时候创建,以及session一致性问题
    IaaS、PaaS、SaaS、DaaS都是什么?现在怎么样了?终于有人讲明白了
    FaaS,未来的后端服务开发之道
    架构师必须了解的30条设计原则
    vuejs2.0使用Sortable.js实现的拖拽功能( 转)
    Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
    Java静态类
    CGLIB(Code Generation Library)详解
    hive--udf函数(开发-4种加载方式)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14191366.html
Copyright © 2020-2023  润新知