JavaWeb-SpringSecurity初认识

 

 

Spring Security 安全  百度百科

  功能:Spring Security对Web安全性的支持大量地依赖于Servlet过滤器。这些过滤器拦截进入请求,并且在应用程序处理该请求之前进行某些安全处理。 Spring Security提供有若干个过滤器,它们能够拦截Servlet请求,并将这些请求转给认证和访问决策管理器处理,从而增强安全性。根据自己的需要,可以使用适当的过滤器来保护自己的应用程序。
  如果使用过Servlet过滤器且令其正常工作,就必须在Web应用程序的web.xml文件中使用<filter> 和<filter-mapping>元素配置它们。虽然这样做能起作用,但是它并不适用于使用依赖注入进行的配置。
  FilterToBeanProxy是一个特殊的Servlet过滤器,它本身做的工作并不多,而是将自己的工作委托给Spring应用程序上下文 中的一个Bean来完成。被委托的Bean几乎和其他的Servlet过滤器一样,实现javax.servlet.Filter接 口,但它是在Spring配置文件而不是web.xml文件中配置的。
  实际上,FilterToBeanProxy代理给的那个Bean可以是javax.servlet.Filter的任意实现。这可以是 Spring Security的任何一个过滤器,或者它可以是自己创建的一个过滤器。但是正如本书已经提到的那样,Spring Security要求至少配置四个而且可能一打或者更多的过滤器。

 

  总而言之:Spring Security 安全将我们的服务器保护起来,访问任何资源都需要身份认证。配置filter,SpringBoot以及自动帮我们配置好,我们不用管,可以把Spring Security理解为一系列拦截器。

 

  整个项目基于

  JavaWeb-RESTful(二)_使用SpringMVC开发RESTful_下  传送门

  

  系列博文

  项目已上传至guthub  传送门

  JavaWeb-SpringSecurity初认识  传送门

  JavaWeb-SpringSecurity在数据库中查询登陆用户  传送门

  JavaWeb-SpringSecurity自定义登陆页面  传送门

  JavaWeb-SpringSecurity实现需求-判断请求是否以html结尾  传送门

  JavaWeb-SpringSecurity自定义登陆配置  传送门

  JavaWeb-SpringSecurity图片验证ImageCode  传送门

  JavaWeb-SpringSecurity记住我功能  传送门

  JavaWeb-SpringSecurity使用短信验证码登陆  传送门

 

  在GaryRESTful.config下创建java安全适配器类SecurityConfig.java

  配置SecurityConfig.java

    //表单验证(身份认证)
    protected void configure(HttpSecurity http) throws Exception{
        http.formLogin()
            .and()
            //请求授权
            .authorizeRequests()
            //所有请求都被拦截,跳转到(/login请求中)
            .anyRequest()
            //都需要我们身份认证
            .authenticated();
    }

 

 

package com.Gary.GaryRESTful.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


//Web应用安全适配器
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    //表单验证(身份认证)
    protected void configure(HttpSecurity http) throws Exception{
        http.formLogin()
            .and()
            //请求授权
            .authorizeRequests()
            //所有请求都被拦截,跳转到(/login请求中)
            .anyRequest()
            //都需要我们身份认证
            .authenticated();
    }
    
}
SecurityConfig.java

 

  spring Security 安全将我们的服务器保护起来,访问任何资源都需要身份认证,配置filter,SpringBoot以及自动帮我们配置好,我们不用管,可以理解为一系列的拦截器(filter)

 

 

 

  如果要设置自己用户登陆的账号密码时,Security5版本规定了不能使用明文做密码

  我们可以使用Security5自带的PasswordEncoder做BCrypt加密设置

  在SecurityConfig.java中配置加密规则

    @Bean
    public PasswordEncoder passwordEncoder()
    {
        return new BCryptPasswordEncoder();
    }

 

  UserService中配置用户登陆账号密码

    @Autowired
    private PasswordEncoder passwordEncoder;
    
    //spring security默认处理登陆(username为输入的username)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // TODO Auto-generated method stub
        System.out.println(username);
        
        return new User(username,passwordEncoder.encode("123456"),AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

 

 

package com.Gary.GaryRESTful.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


//Web应用安全适配器
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    //告诉SpringSecurity密码用什么加密的
    @Bean
    public PasswordEncoder passwordEncoder()
    {
        return new BCryptPasswordEncoder();
    }
    
    
    //表单验证(身份认证)
    protected void configure(HttpSecurity http) throws Exception{
        http.formLogin()
            .and()
            //请求授权
            .authorizeRequests()
            //所有请求都被拦截,跳转到(/login请求中)
            .anyRequest()
            //都需要我们身份认证
            .authenticated();
    }
    
}
SecurityConfig.java

 

package com.Gary.GaryRESTful.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;


//用SprinSecurity默认的登陆系统
//UserService要实现UserDetailsService接口
@Component
public class UserService implements UserDetailsService{

    @Autowired
    private PasswordEncoder passwordEncoder;
    
    //spring security默认处理登陆(username为输入的username)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // TODO Auto-generated method stub
        System.out.println(username);
        
        //用户名,密码,权限
        //User实现UserDetails接口
        return new User(username,passwordEncoder.encode("123456"),AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }

    
    
}
UserService.java

 

posted @ 2019-10-26 16:43  Cynical丶Gary  阅读(351)  评论(0编辑  收藏  举报