spring boot Security is simple to use

spring boot Security is simple to use

  1. The introduction of dependence

    <!-- security -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  2. 配置 SecurityConfigbr/>@Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

            @Autowired
            UserDetailServiceImpl userDetailService;
            @Autowired
            LoginSuccessHandler loginSuccessHandler;
    
            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                    //自定义用户验证和加密方式
                    auth.userDetailsService(userDetailService).passwordEncoder(new BCryptPasswordEncoder());
            }
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                    http.formLogin()                    //  定义当需要用户登录时候,转到的登录页面。
            //          .loginPage("/login.html") //自定义登录页面
    //                .loginProcessingUrl("/login") //自定义登录接口地址
                                    .successHandler(loginSuccessHandler)
                                    .and()
                                    // 定义哪些URL需要被保护、哪些不需要被保护
                                    .authorizeRequests().antMatchers("/login").permitAll() //不需要保护的URL
                                    .anyRequest()               // 任何请求,登录后可以访问
                                    .authenticated()
                                    .and()
                                    .logout().logoutSuccessUrl("/login").permitAll() // 登出
                                    .and()
                                    .csrf().disable();
            }
    }

3. User authentication process

        @Component
        public class UserDetailServiceImpl implements UserDetailsService {
                /**
                 * 用户校验
                 * @param s
                 * @return
                 * @throws UsernameNotFoundException
                 */
                @Override
                public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
                        Collection<GrantedAuthority> collection = new ArrayList<>();//权限集合
                        String password = new BCryptPasswordEncoder().encode("123456");
                        User user = new User(s,password,collection);

                        return user;
                }

        }

4. After successful login process

@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

                httpServletResponse.setContentType("application/json;charset=UTF-8");

                httpServletResponse.getWriter().write(authentication.getName());
        }
}

HttpSecurity there is still a class function that can be used
, please refer to:
https://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/apidocs/org/springframework/security/config/annotation/web/ builders / HttpSecurity.html

----end----

Guess you like

Origin blog.51cto.com/5013162/2404946