快速上手Spring Security权限

为什么需要权限管理?

安全性:误操作,人为破坏,数据泄露等

数据隔离:不同权限能看到及操作不同的数据

明确职责:运营,客服等不同角色,等级不同


权限管理的核心是什么?

1.用户-权限:人员少,功能固定,或特别简单的系统

2.RBAC:用户-角色-权限 所有系统都适用



权限框架主要有?

1. Spring Security

2.apache shiro

本博客主要讲Spring Security

Spring Security


主要是认证和验证


Basic 认证:Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用。


Digest 认证:客户端请求资源->服务器返回认证标示->客户端发送认证信息->服务器查验认证,如果成功则继续资源传送,否则直接断开连接。


x.509认证:数字认证


LDAP 认证:LDAP认证是通过WSS3.0加上轻量目录LDAP协议搭建的种认证方式,使用https加密传输,主要用于做文档管理。


From 认证:表单认证




下面使用springboot+Spring Security 实战下


Spring Security实战

1.搭建springbooot环境

https://start.spring.io/ 搭建springboot网站,选择如下两个依赖,我这里使用1.5.6.RELEASE的springboot


搭建好了之后下载下来解压,导入IDEA 中


运行项目,能成功启动即可,如下不报错,就说明这个工程没有问题。



2.只要能登录即可的例子

在主程序DemoApplication中编写两个方法,一个是用来访问主页用(home()),一个是需要登录才能访问(hello())
package com.su.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {


    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


    @RequestMapping("/")
    public String home() {
        return "您好,主页";
    }


    @RequestMapping("/hello")
    public String hello() {
        return "hello路径";
    }
}


新建一个方法SpringSecurityConfig ,主要是用户进行权限设置的
package com.su.demo;


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


/**
 *主要是权限配置
 */
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * 拦截下面的东西
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll() //主路径允许访问
                .anyRequest().authenticated()  //验证
                .and()
                .logout().permitAll() //注销也是运行访问
                .and()
                .formLogin();
        http.csrf().disable();  //关闭csrf() 认证
    }


    /**
     * 不拦截下面这些资源
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
    }
}

运行程序,效果:运行**/** 允许访问 ,运行/hello 是需要被拦截的



3.基_于内存的权限设置_

在方法SpringSecurityConfig 中添加代码
/**
 * 基于内存的验证:不需要用到数据库的情况
 *
 * @param auth
 * @throws Exception
 */
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //我们指定一个人这个人的用户:admin 密码:123456  角色:ADMIN
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");


}


当访问http://localhost:8080/hello我们需要输入存在内存中的用户名密码


4.角色进行拦截

在方法DemoApplication 中添加代码

@EnableGlobalMethodSecurity(prePostEnabled = true)//只有ADMIN 的角色才能访问

/**
 * 只有ADMIN 的角色才能访问
 * @return
 */
@PreAuthorize("hasRole('ROLE_ADMIN')")//必须用ROLE_xxx  才能识别你是做角色权限
@RequestMapping("/roleAuth")
public String role () {
    return "需要某个权限才能访问";
}


运行,当访问http://localhost:8080/roleAuth时只有角色为ADMIN 的用户才能访问


#春招##内推##实习##面经##Java##社招##技术栈##学习路径#
全部评论
我主学shiro可以嘛,其他的了解了解
点赞 回复 分享
发布于 2022-06-03 11:55
安全是现在大家都比较关注的问题啊
点赞 回复 分享
发布于 2022-05-23 17:40

相关推荐

真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
大专境巅峰电子狗:头一次看到这种简历,学术与技术学习,直接用技能概括就好了呀,实习经历要写丰富一点
点赞 评论 收藏
分享
评论
7
6
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务