spring security(四)

 

Spring Boot

有了Spring Boot这样的神器,能够很简单的使用强大的Spring框架。你须要关心的事儿只是建立应用,没必要再配置了,“Just run!”,这但是Josh Long每次演讲必说的,他的另外一句必须说的就是“make jar not war”,这意味着,不用太关心是Tomcat仍是Jetty或者Undertow了。专心解决逻辑问题,这固然是个好事儿,部署简单了不少。html

建立Spring Boot应用

有不少方法去建立Spring Boot项目,官方也推荐用:git

start.spring.io能够方便选择你要用的组件,命令行工具固然也能够。目前Spring Boot已经到了1.53,我是懒得去更新依赖,继续用1.52版本。虽然阿里也有了中央库的国内版本不知道是否稳定。若是你感兴趣,能够本身尝试下。你能够选Maven或者Gradle成为你项目的构建工具,Gradle优雅一些,使用了Groovy语言进行描述。github

打开start.spring.io,建立的项目只须要一个Dependency,也就是Web,而后下载项目,用IntellJ IDEA打开。个人Java版本是1.8。web

这里看下整个项目的pom.xml文件中的依赖部分:spring

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

全部Spring Boot相关的依赖都是以starter形式出现,这样你无需关心版本和相关的依赖,因此这样大大简化了开发过程。数据库

当你在pom文件中集成了spring-boot-maven-plugin插件后你可使用Maven相关的命令来run你的应用。例如mvn spring-boot:run,这样会启动一个嵌入式的Tomcat,并运行在8080端口,直接访问你固然会得到一个Whitelabel Error Page,这说明Tomcat已经启动了。json

建立一个Web 应用

这仍是一篇关于Web安全的文章,可是也得先有个简单的HTTP请求响应。咱们先弄一个能够返回JSON的Controller。修改程序的入口文件:api

@SpringBootApplication
@RestController
@EnableAutoConfiguration
public class DemoApplication {

    // main函数,Spring Boot程序入口
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    // 根目录映射 Get访问方式 直接返回一个字符串
    @RequestMapping("/")
    Map<String, String> hello() {
      // 返回map会变成JSON key value方式
      Map<String,String> map=new HashMap<String,String>();
      map.put("content", "hello freewolf~");
      return map;
    }
}

这里我尽可能的写清楚,让不了解Spring Security的人经过这个例子能够了解这个东西,不少人都以为它很复杂,而投向了Apache Shiro,其实这个并不难懂。知道主要的处理流程,和这个流程中哪些类都起了哪些做用就行了。浏览器

Spring Boot对于开发人员最大的好处在于能够对Spring应用进行自动配置。Spring Boot会根据应用中声明的第三方依赖来自动配置Spring框架,而不须要进行显式的声明。Spring Boot推荐采用基于Java注解的配置方式,而不是传统的XML。只须要在主配置 Java 类上添加@EnableAutoConfiguration注解就能够启用自动配置。Spring Boot的自动配置功能是没有侵入性的,只是做为一种基本的默认实现。安全

这个入口类咱们添加@RestController@EnableAutoConfiguration两个注解。
@RestController注解至关于@ResponseBody@Controller合在一块儿的做用。

run整个项目。访问http://localhost:8080/就能看到这个JSON的输出。使用Chrome浏览器能够装JSON Formatter这个插件,显示更PL一些。

{
  "content": "hello freewolf~"
}

为了显示统一的JSON返回,这里创建一个JSONResult类进行,简单的处理。首先修改pom.xml,加入org.json相关依赖。

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
</dependency>

而后在咱们的代码中加入一个新的类,里面只有一个结果集处理方法,由于只是个Demo,全部这里都放在一个文件中。这个类只是让返回的JSON结果变为三部分:

  • status - 返回状态码 0 表明正常返回,其余都是错误
  • message - 通常显示错误信息
  • result - 结果集
class JSONResult{
    public static String fillResultString(Integer status, String message, Object result){
        JSONObject jsonObject = new JSONObject(){{
            put("status", status);
            put("message", message);
            put("result", result);
        }};
        return jsonObject.toString();
    }
}

而后咱们引入一个新的@RestController并返回一些简单的结果,后面咱们将对这些内容进行访问控制,这里用到了上面的结果集处理类。这里多放两个方法,后面咱们来测试权限和角色的验证用。

@RestController
class UserController {

    // 路由映射到/users
    @RequestMapping(value = "/users", produces="application/json;charset=UTF-8")
    public String usersList() {

        ArrayList<String> users =  new ArrayList<String>(){{
            add("freewolf");
            add("tom");
            add("jerry");
        }};

        return JSONResult.fillResultString(0, "", users);
    }

    @RequestMapping(value = "/hello", produces="application/json;charset=UTF-8")
    public String hello() {
        ArrayList<String> users =  new ArrayList<String>(){{ add("hello"); }};
        return JSONResult.fillResultString(0, "", users);
    }

    @RequestMapping(value = "/world", produces="application/json;charset=UTF-8")
    public String world() {
        ArrayList<String> users =  new ArrayList<String>(){{ add("world"); }};
        return JSONResult.fillResultString(0, "", users);
    }
}

从新run这个文件,访问http://localhost:8080/users就看到了下面的结果:

{
  "result": [
    "freewolf",
    "tom",
    "jerry"
  ],
  "message": "",
  "status": 0
}

若是你细心,你会发现这里的JSON返回时,Chrome的格式化插件好像并无识别?这是为何呢?咱们借助curl分别看一下咱们写的两个方法的Header信息.

curl -I http://127.0.0.1:8080/
curl -I http://127.0.0.1:8080/users

能够看到第一个方法hello,因为返回值是Map,Spring已经有相关的机制自动处理成JSON:

Content-Type: application/json;charset=UTF-8

第二个方法usersList因为返回时String,因为是@RestControler已经含有了@ResponseBody也就是直接返回内容,并不模板。因此就是:

Content-Type: text/plain;charset=UTF-8

那怎么才能让它变成JSON呢,其实也很简单只须要补充一下相关注解:

@RequestMapping(value = "/users", produces="application/json;charset=UTF-8")

这样就行了。

使用JWT保护你的Spring Boot应用

终于咱们开始介绍正题,这里咱们会对/users进行访问控制,先经过申请一个JWT(JSON Web Token读jot),而后经过这个访问/users,才能拿到数据。

关于JWT,出门奔向如下内容,这些不在本文讨论范围内:

JWT很大程度上仍是个新技术,经过使用HMAC(Hash-based Message Authentication Code)计算信息摘要,也能够用RSA公私钥中的私钥进行签名。这个根据业务场景进行选择。

添加Spring Security

根据上文咱们说过咱们要对/users进行访问控制,让用户在/login进行登陆并得到Token。这里咱们须要将spring-boot-starter-security加入pom.xml。加入后,咱们的Spring Boot项目将须要提供身份验证,相关的pom.xml以下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.7.0</version>
</dependency>

至此咱们以前全部的路由都须要身份验证。咱们将引入一个安全设置类WebSecurityConfig,这个类须要从WebSecurityConfigurerAdapter类继承。

@Configuration
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    // 设置 HTTP 验证规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭csrf验证
        http.csrf().disable()
                // 对请求进行认证
                .authorizeRequests()
                // 全部 / 的全部请求 都放行
                .antMatchers("/").permitAll()
                // 全部 /login 的POST请求 都放行
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                // 权限检查
                .antMatchers("/hello").hasAuthority("AUTH_WRITE")
                // 角色检查
                .antMatchers("/world").hasRole("ADMIN")
                // 全部请求须要身份认证
                .anyRequest().authenticated()
            .and()
                // 添加一个过滤器 全部访问 /login 的请求交给 JWTLoginFilter 来处理 这个类处理全部的JWT相关内容
                .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
                        UsernamePasswordAuthenticationFilter.class)
                // 添加一个过滤器验证其余请求的Token是否合法
                .addFilterBefore(new JWTAuthenticationFilter(),
                        UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 使用自定义身份验证组件
        auth.authenticationProvider(new CustomAuthenticationProvider());

    }
}

先放两个基本类,一个负责存储用户名密码,另外一个是一个权限类型,负责存储权限和角色。

class AccountCredentials {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

class GrantedAuthorityImpl implements GrantedAuthority{
    private String authority;

    public GrantedAuthorityImpl(String authority) {
        this.authority = authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

    @Override
    public String getAuthority() {
        return this.authority;
    }
}

在上面的安全设置类中,咱们设置全部人都能访问/POST方式访问/login,其余的任何路由都须要进行认证。而后将全部访问/login的请求,都交给JWTLoginFilter过滤器来处理。稍后咱们会建立这个过滤器和其余这里须要的JWTAuthenticationFilterCustomAuthenticationProvider两个类。

先创建一个JWT生成,和验签的类

class TokenAuthenticationService {
    static final long EXPIRATIONTIME = 432_000_000;     // 5天
    static final String SECRET = "P@ssw02d";            // JWT密码
    static final String TOKEN_PREFIX = "Bearer";        // Token前缀
    static final String HEADER_STRING = "Authorization";// 存放Token的Header Key

  // JWT生成方法
    static void addAuthentication(HttpServletResponse response, String username) {

    // 生成JWT
        String JWT = Jwts.builder()
                // 保存权限(角色)
                .claim("authorities", "ROLE_ADMIN,AUTH_WRITE")
                // 用户名写入标题
                .setSubject(username)
                // 有效期设置
                        .setExpiration(new Date(System.currentTimeMillis() + EXPIRATIONTIME))
                // 签名设置
                        .signWith(SignatureAlgorithm.HS512, SECRET)
                        .compact();

        // 将 JWT 写入 body
        try {
            response.setContentType("application/json");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getOutputStream().println(JSONResult.fillResultString(0, "", JWT));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  // JWT验证方法
    static Authentication getAuthentication(HttpServletRequest request) {
        // 从Header中拿到token
        String token = request.getHeader(HEADER_STRING);

        if (token != null) {
            // 解析 Token
            Claims claims = Jwts.parser()
                    // 验签
                    .setSigningKey(SECRET)
                    // 去掉 Bearer
                    .parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
                    .getBody();

            // 拿用户名
            String user = claims.getSubject();

            // 获得 权限(角色)
            List<GrantedAuthority> authorities =  AuthorityUtils.commaSeparatedStringToAuthorityList((String) claims.get("authorities"));

            // 返回验证令牌
            return user != null ?
                    new UsernamePasswordAuthenticationToken(user, null, authorities) :
                    null;
        }
        return null;
    }
}

这个类就两个static方法,一个负责生成JWT,一个负责认证JWT最后生成验证令牌。注释已经写得很清楚了,这里很少说了。

下面来看自定义验证组件,这里简单写了,这个类就是提供密码验证功能,在实际使用时换成本身相应的验证逻辑,从数据库中取出、比对、赋予用户相应权限。

// 自定义身份认证验证组件
class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        // 获取认证的用户名 & 密码
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();

        // 认证逻辑
        if (name.equals("admin") && password.equals("123456")) {

            // 这里设置权限和角色
            ArrayList<GrantedAuthority> authorities = new ArrayList<>();
            authorities.add( new GrantedAuthorityImpl("ROLE_ADMIN") );
            authorities.add( new GrantedAuthorityImpl("AUTH_WRITE") );
            // 生成令牌
            Authentication auth = new UsernamePasswordAuthenticationToken(name, password, authorities);
            return auth;
        }else {
            throw new BadCredentialsException("密码错误~");
        }
    }

    // 是否能够提供输入类型的认证服务
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

下面实现JWTLoginFilter 这个Filter比较简单,除了构造函数须要重写三个方法。

  • attemptAuthentication - 登陆时须要验证时候调用
  • successfulAuthentication - 验证成功后调用
  • unsuccessfulAuthentication - 验证失败后调用,这里直接灌入500错误返回,因为同一JSON返回,HTTP就都返回200了
class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {

    public JWTLoginFilter(String url, AuthenticationManager authManager) {
        super(new AntPathRequestMatcher(url));
        setAuthenticationManager(authManager);
    }

    @Override
    public Authentication attemptAuthentication(
            HttpServletRequest req, HttpServletResponse res)
            throws AuthenticationException, IOException, ServletException {

        // JSON反序列化成 AccountCredentials
        AccountCredentials creds = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class);

        // 返回一个验证令牌
        return getAuthenticationManager().authenticate(
                new UsernamePasswordAuthenticationToken(
                        creds.getUsername(),
                        creds.getPassword()
                )
        );
    }

    @Override
    protected void successfulAuthentication(
            HttpServletRequest req,
            HttpServletResponse res, FilterChain chain,
            Authentication auth) throws IOException, ServletException {

        TokenAuthenticationService.addAuthentication(res, auth.getName());
    }


    @Override
    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {

        response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getOutputStream().println(JSONResult.fillResultString(500, "Internal Server Error!!!", JSONObject.NULL));
    }
}

再完成最后一个类JWTAuthenticationFilter,这也是个拦截器,它拦截全部须要JWT的请求,而后调用TokenAuthenticationService类的静态方法去作JWT验证。

class JWTAuthenticationFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain filterChain)
            throws IOException, ServletException {
        Authentication authentication = TokenAuthenticationService
                .getAuthentication((HttpServletRequest)request);

        SecurityContextHolder.getContext()
                .setAuthentication(authentication);
        filterChain.doFilter(request,response);
    }
}

如今代码就写完了,整个Spring Security结合JWT基本就差很少了,下面咱们来测试下,并说下总体流程。

开始测试,先运行整个项目,这里介绍下过程:

  • 先程序启动 - main函数
  • 注册验证组件 - WebSecurityConfigconfigure(AuthenticationManagerBuilder auth)方法,这里咱们注册了自定义验证组件
  • 设置验证规则 - WebSecurityConfigconfigure(HttpSecurity http)方法,这里设置了各类路由访问规则
  • 初始化过滤组件 - JWTLoginFilterJWTAuthenticationFilter 类会初始化

首先测试获取Token,这里使用CURL命令行工具来测试。

curl -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"123456"}'  http://127.0.0.1:8080/login

结果:

{
  "result": "eyJhbGciOiJIUzUxMiJ9.eyJhdXRob3JpdGllcyI6IlJPTEVfQURNSU4sQVVUSF9XUklURSIsInN1YiI6ImFkbWluIiwiZXhwIjoxNDkzNzgyMjQwfQ.HNfV1CU2CdAnBTH682C5-KOfr2P71xr9PYLaLpDVhOw8KWWSJ0lBo0BCq4LoNwsK_Y3-W3avgbJb0jW9FNYDRQ",
  "message": "",
  "status": 0
}

这里咱们获得了相关的JWT,反Base64以后,就是下面的内容,标准JWT

{"alg":"HS512"}{"authorities":"ROLE_ADMIN,AUTH_WRITE","sub":"admin","exp":1493782240} ͽ]BS` pS 6~hCV H%  
ܬ)֝ଖoE5р

整个过程以下:

  • 拿到传入JSON,解析用户名密码 - JWTLoginFilterattemptAuthentication 方法
  • 自定义身份认证验证组件,进行身份认证 - CustomAuthenticationProviderauthenticate 方法
  • 盐城成功 - JWTLoginFiltersuccessfulAuthentication 方法
  • 生成JWT - TokenAuthenticationServiceaddAuthentication方法

再测试一个访问资源的:

curl -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJhdXRob3JpdGllcyI6IlJPTEVfQURNSU4sQVVUSF9XUklURSIsInN1YiI6ImFkbWluIiwiZXhwIjoxNDkzNzgyMjQwfQ.HNfV1CU2CdAnBTH682C5-KOfr2P71xr9PYLaLpDVhOw8KWWSJ0lBo0BCq4LoNwsK_Y3-W3avgbJb0jW9FNYDRQ"  http://127.0.0.1:8080/users

结果:

{
  "result":["freewolf","tom","jerry"],
  "message":"",
  "status":0
}

说明咱们的Token生效能够正常访问。其余的结果您能够本身去测试。再回处处理流程:

  • 接到请求进行拦截 - JWTAuthenticationFilter 中的方法
  • 验证JWT - TokenAuthenticationServicegetAuthentication 方法
  • 访问Controller

这样本文的主要流程就结束了,本文主要介绍了,如何用Spring Security结合JWT保护你的Spring Boot应用。如何使用RoleAuthority,这里多说一句其实在Spring Security中,对于GrantedAuthority接口实现类来讲是不区分是Role仍是Authority,两者区别就是若是是hasAuthority判断,就是判断整个字符串,判断hasRole时,系统自动加上ROLE_到判断的Role字符串上,也就是说hasRole("CREATE")hasAuthority('ROLE_CREATE')是相同的。利用这些能够搭建完整的RBAC体系。本文到此,你已经会用了本文介绍的知识点。

代码整理后我会上传到Github

本文代码

github.com/freew01f/se…

参考资源

相关文章
相关标签/搜索