详解springsecurity之httpSecurity使用示例-创新互联

httpSecurity

创新互联专业为企业提供沙雅网站建设、沙雅做网站、沙雅网站设计、沙雅网站制作等企业网站建设、网页设计与制作、沙雅企业网站模板建站服务,十载沙雅做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

类似于spring security的xml配置文件命名空间配置中的元素。它允许对特定的http请求基于安全考虑进行配置。默认情况下,适用于所有的请求,但可以使用requestMatcher(RequestMatcher)或者其它相似的方法进行限制。

使用示例:

最基本的基于表单的配置如下。该配置将所有的url访问权限设定为角色名称为"ROLE_USER".同时也定义了内存认证模式:使用用户名"user"和密码“password”,角色"ROLE_USER"来认证。

 @Configuration
 @EnableWebSecurity
 public class FormLoginSecurityConfig extends WebSecurityConfigurerAdapter {
 
  @Override
  protected void configure(HttpSecurity http) throws Exception {
   http
    .authorizeRequests()
     .antMatchers("/").hasRole("USER")
     .and()
    .formLogin();
  }
 
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   auth
    .inMemoryAuthentication()
     .withUser("user")
       .password("password")
       .roles("USER");
  }
 }

当前题目:详解springsecurity之httpSecurity使用示例-创新互联
网页网址:http://scyanting.com/article/dsppgh.html