spring Security安全框架

spring Security安全框架

基于springBoot框架

spring Security能做什么

1.认证,实现登录安全功能
2.授权,确认在当前系统下用户所拥有的功能权限

spring Security如何运行

1.在pom.xml中引入spring-boot-starter-security

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

效果
在浏览器中输入 http://localhost:8080 会自动跳到spring Security的登录页面,username默认为user,password为后台给你的密码 如下代码;默认拦截所有的url
在这里插入图片描述

2020-02-11 12:42:32.656  INFO 1084 --- [  restartedMain] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: 5c34ca30-49e4-4bd8-9db7-6d69b4bd0582

spring Security的原理

通过一系列的filter来保护web资源;对API接口则使用AOP来保护
在这里插入图片描述每个Filter所代表的含义
在这里插入图片描述

spring Security的配置

1.开启过滤器支持

只需要让自己的类继承AbstractSecurityWebApplicationInitializer

2.配置

让自己的类继承WebSecurityConfigurerAdapter

3.用户认证

我们通过重写protected void configure(AuthenticationManagerBuilder auth)方法来实现定制;这个方法在继承了WebSecurityConfigurerAdapter类的配置类中

3.1内存中的用户

通过inMemoryAuthentication方法来添加

 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //userDetailsService(userDetailsService) 认证所采用的方式
        //passwordEncoder(passwordEncoder())      加密解密所采用的方式
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        auth.inMemoryAuthentication()
                .withUser("wyf").password("wyf").roles("ROLE_ADMIN");
                .and()
                .withUser("wyf").password("wyf").roles("ROLE_USER");
    }

3.2通用的用户

需要自定义实现UserDetailsService接口

public class userDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private userServerce userServerce;
    @Autowired
    private permissionDao permissionDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        user user = userServerce.getuser(username);
        if(user == null){
            throw new AuthenticationCredentialsNotFoundException("用户名不存在");
        }else if (user.getStatus() == com.mannnger.model.user.Status.DISABLED){
            throw new LockedException("用户被锁定,请联系管理员");
        }

        loginUserDto loginUserDto = new loginUserDto();
        BeanUtils.copyProperties(user,loginUserDto);
        loginUserDto.setPermissions(permissionDao.listByUserId(user.getId().longValue()));
        System.out.println(permissionDao.listByUserId(user.getId().longValue()));


        return loginUserDto;
    }
}

然后在配置类中引入自定义实现的UserDetailsService接口*

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //userDetailsService(userDetailsService) 认证所采用的方式
        //passwordEncoder(passwordEncoder())      加密解密所采用的方式
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());

    }

4.请求授权

我们通过重写protected void configure(HttpSecurity http)方法来实现

protected void configure(HttpSecurity http) throws Exception {
			http.authorizeRequests()//1 
                .antMatchers("/login.html","/user/add")//2
                .permitAll()//3
                .anyRequest()//4
                .authenticated()//5;
                }
              

1:通过authorizeRequests方法来开始请求权限配置
2.请求匹配的资源
3.用户可任意访问
4.匹配所有请求路径,除去前面已匹配过的
5.用户登录后可访问

5.定制登录行为

protected void configure(HttpSecurity http) throws Exception {
			http.formLogin() //1
                .loginPage("/login.html")//指定登录的html页面
                .loginProcessingUrl("/login")//指定的处理路径
                .successHandler(myAuthenticationSuccessHander)//2
                .defaultSuccessUrl("/index");
                .failureHandler(myAuthenctiationFailureHandler)//3
                //退出处理
                .and().logout()//4
                    .permitAll()
                .invalidateHttpSession(true)//注销session
                .deleteCookies("JSESSIONID")
                .logoutSuccessHandler(myLogoutSuccessHander);//5

1.通过formLogin方法定制登录操作
2.定制登录成功后的处理类
defaultSuccessUrl("/index");指定登录后转向的页面
3.定制登录失败后的处理类
4.使用logout方法定制注销行为
5.指定注销成功后转向的页面

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值