Spring Security(安全)

在web开发中,安全第一位!拦截器、过滤器、安全框架

Spring Security

     Spring Security是针对springboot项目的安全框架,也是Spring Boot低层安全模块默认的技术选型,他可以实现强大的web控制,对于安全控制,我们仅需要引入spring-boot-stater-security模块,进行少量的配置来实现强大的安全管理!
记住几个类:

  • WebSecurityConfigureAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity: 开启WebSecurity模式

Spring Security的两个主要目标是“认证”和“授权”

依赖

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

登录、注销

// aop :拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 请求设置角色
        http.authorizeRequests().antMatchers("/index").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

    //  设置自定义登录界面,
    // loginPage 登录界面
    // loginProcessingUrl 登录时所发的请求与登录界面action同步
    // defaultSuccessUrl 登录成功后的请求
        http.formLogin().loginPage("/tologin").loginProcessingUrl("/tologin").defaultSuccessUrl("/toindex",true);
        // 跨站请求伪造 --何为跨站请求伪造自行百度
        http.csrf().disable();
        // 注销  
        // logout Security自带一个/logout的请求,只需将注销请求地址设置为其自带的即可
        // logoutSuccessUrl 注销后所发请求
        http.logout().logoutSuccessUrl("/index");
    // 记住我 cookie 默认两周     默认参数名remember-me可自定义
        http.rememberMe().rememberMeParameter("rememme");
    }

     @Override
     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //方式一:内存  账号设置角色
         auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                 .withUser("developer").password(new BCryptPasswordEncoder().encode("1")).roles("vip1", "vip2", "vip3")
                 .and()
                 .withUser("xusx").password(new BCryptPasswordEncoder().encode("1")).roles("vip1");
         // 方式二:
         //  auth.userDetailsService(authUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());

     }
 }
方式二:连接数据库

1、Bean

public class Users {
    private int id;
    private String username;
    private String password;
    private String  role;

2、usersService

@Mapper
@Repository
public interface UsersService {
    Users findUser(@PathParam("username") String username);
}

3、核心

@Component
public class AuthUserDetailsService  implements UserDetailsService {
    @Autowired
    UsersService usersService;


    @Override
    public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException   {
        Users userInfo = usersService.findUser(username);
        if(userInfo==null){
                throw  new UsernameNotFoundException("当前用户不存在");
        }
        List<GrantedAuthority> grantedAuthorityList=new ArrayList<>();
        grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_"+userInfo.getRole()));
        return new User(userInfo.getUsername(),new BCryptPasswordEncoder().encode(userInfo.getPassword()),grantedAuthorityList);
    }
}

4、上文SecurityConfig

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值