java中Spring Security的实例详解

  

<强> java中Spring Security的实例详解

  

spring security是一个多方面的安全认证框架,提供了基于JavaEE规范的完整的安全认证解决方案,并且可以很好与目前主流的认证框架(如中科院中央授权系统)集成。使用spring security的初衷是解决不同用户登录不同应用程序的权限问题,说到权限包括两部分:认证和授权。认证是告诉系统你是谁,授权是指知道你是谁后是否有权限访问系统(授权后一般会在服务端创建一个令牌,之后用这个令牌进行后续行为的交互)。

  

spring security提供了多种认证模式,很多第三方的认证技术都可以很好集成:

  
      <李>基于表单的认证(用于简单的用户界面)   <李> OpenID认证   <李>认证基?   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-security   & lt; version> 1.5.1.RELEASE   & lt;/dependency>   之前      

    配置:         @ configuration   @EnableWebSecurity   公开课SecurityConfig延伸WebSecurityConfigurerAdapter {      @ bean   公共UserDetailsService UserDetailsService () {   返回新CustomUserDetailsService ();   }      @Override   保护无效配置(AuthenticationManagerBuilder auth)抛出异常{   auth.inMemoryAuthentication () .withUser (rhwayfun) .password .roles(“1209”)(“用户”)   ,().withUser (“admin”) .password .roles (“123456”) (“admin”);//auth.jdbcAuthentication () .dataSource (securityDataSource);//auth.userDetailsService (userDetailsService ());   }      @Override   保护无效配置(HttpSecurity http){抛出异常   http.authorizeRequests()//配置安全策略//.antMatchers(“/薄ⅰ?指数”).permitAll()//定义/请求不需要验证   .authenticated .anyRequest()()//其余的所有请求都需要验证   , ()   .formLogin ()   .loginPage("/登录”)   .defaultSuccessUrl(“/指数”)   .permitAll ()   , ()   .logout ()   .logoutSuccessUrl("/登录”)   .permitAll();//定义注销不需要验证      .disable http.csrf () ();   }      }      之前      

    这里需要覆盖WebSecurityConfigurerAdapter的两个方法,分别定义什么请求需要什么权限,并且认证的用户密码分别是什么。

            @ configuration   公开课WebMvcConfig延伸WebMvcConfigurerAdapter {/* *   *统一注册纯RequestMapping跳转视图的控制器   */@Override   公共空间addViewControllers (ViewControllerRegistry注册表){   registry.addViewController .setViewName("/登录")("/登录");   }   }   之前      

    添加登录跳转的URL,如果不加这个配置也会默认跳转到/登录下,所以这里还可以自定义登录的请求路径。

      

    登录页面:

            & lt; !DOCTYPE html>      & lt; %, & lt; %=@ taglib前缀“春天”uri=" http://www.springframework.org/tags " %比;   & lt; % @ taglib前缀=" c " uri=" http://java.sun.com/jsp/jstl/core " %祝辞——%比;      & lt; html>   & lt; head>   & lt;元charset=皍tf - 8”比;   & lt; title>欢迎SpringBoot   & lt;脚本src=" https://www.yisu.com/js/jquery-3.1.1.min.js "祝辞& lt;/script>   & lt;脚本src=" https://www.yisu.com/js/index.js "祝辞& lt;/script>   & lt;/head>   & lt; body>      & lt;表单名称=" f " action="/登录" method=" post "比;   & lt;输入id="名称" name="用户名" type=" text "/祝辞& lt; br>   & lt;输入id="密码" name="密码" type="密码"祝辞& lt; br>   & lt;输入类型="提交" value=" https://www.yisu.com/zixun/login "比;   & lt;输入name=" _csrf类型=耙亍眝alue=" https://www.yisu.com/zixun/$ {_csrf} "/比;   & lt;/form>      & lt; p id=坝没А痹?      & lt;/p>      & lt; script>   $(函数(){   $ (“[name=f]”) .focus ()   })   & lt;/script>   & lt;/body>      & lt;/html>      之前      

    <>强基于内存

      

    SecurityConfig这个配置已经是基于了内存中的用户进行认证的,

            auth.inMemoryAuthentication()//基于内存进行认证   .withUser .password (“rhwayfun”)(“1209”)//用户名密码   .roles(“用户”)//用户角色   , ()   .withUser (“admin”) .password .roles (" 123456 ") (" admin ");//admin角色   之前      

    java中Spring Security的实例详解