最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot與spring security的結(jié)合的示例

 更新時(shí)間:2018年03月27日 08:36:30   作者:數(shù)齊  
這篇文章主要介紹了SpringBoot與spring security的結(jié)合的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

權(quán)限控制,也是我們?cè)偃粘i_發(fā)中經(jīng)常遇到的場(chǎng)景,需要根據(jù)用戶的角色決定是否可以看到某個(gè)資源。目前,市面上此類框架主要有shiro與我們今天要講到的spring security。關(guān)于權(quán)限的控制有復(fù)雜的控制,例如幾乎每個(gè)公司都有單點(diǎn)登錄系統(tǒng),根據(jù)用戶名來(lái)到數(shù)據(jù)庫(kù)中拿到對(duì)應(yīng)的權(quán)限,在展示該權(quán)限下能看到的資源。還有一種就是簡(jiǎn)單的控制,也就是我們今天所要提到的。將賬號(hào),密碼,角色配置到代碼中,也可以進(jìn)行簡(jiǎn)單的控制,缺點(diǎn)不言而喻,擴(kuò)展性不好,只有固定的賬號(hào),但是作為演示還是夠用的。

好了廢話不多說(shuō),上pom

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

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

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

spring-boot-starter-security里面包裝了spring security所需要的依賴。不需要我們一個(gè)個(gè)的配置,簡(jiǎn)化了我們的操作,節(jié)省了我們的時(shí)間,不得不說(shuō),這些企業(yè)級(jí)框架考慮的就是很周到,如果我們自己添加jar,可能會(huì)因?yàn)榘姹局g的不兼容而爆出各種問(wèn)題,這都是題外話,贊嘆一下,我們繼續(xù)??聪屡渲妙?/p>

package com.shuqi;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
public class SecurityConfig {

  @Configuration
  public static class WebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
      http
          .authorizeRequests()
          .antMatchers(
              "/index"
          ).hasRole("ADMIN")
          .anyRequest().permitAll()
          .and()
          .httpBasic()
      ;
    }
  }
}

這段配置翻譯成中文是:對(duì)于訪問(wèn)/index這個(gè)鏈接需要ADMIN權(quán)限,其他的全都都允許。有的時(shí)候我們只會(huì)注意代碼,其實(shí)這個(gè)注解@EnableWebSecurity會(huì)重要更多,因?yàn)樗莝pring security的開始,他引入了諸多的配置類,才使得security生效。我們?cè)O(shè)置了ADMIN權(quán)限,但是沒有設(shè)置ADMIN權(quán)限對(duì)應(yīng)的用戶名密碼,所以看下配置文件

security:
 user:
  name: root
  password: root
  role: ADMIN

配置都差不多了,看一眼我們的Controller

package com.shuqi.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
  @RequestMapping("/index")
  public String index(){
    return "hello world index";
  }

  @RequestMapping("/index1")
  public String index1(){
    return "hello world index1";
  }
}

一個(gè)被攔截的/index,一個(gè)不會(huì)被攔截的/index1,看下區(qū)別。啟動(dòng)項(xiàng)目,訪問(wèn)/index

可以看到已經(jīng)加了訪問(wèn)控制,輸入配置的root,root

可以看到結(jié)果

輸入/index1可以直接看到結(jié)果


說(shuō)明我們的配置生效了,spring security確實(shí)幫助我們做到了訪問(wèn)的控制。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

武穴市| 安国市| 山东省| 蓝山县| 霍林郭勒市| 海兴县| 根河市| 盖州市| 含山县| 咸阳市| 旌德县| 辰溪县| 磐石市| 麻城市| 铅山县| 宣武区| 左云县| 安达市| 万年县| 哈密市| 清徐县| 通化市| 泊头市| 和平县| 平武县| 胶州市| 阜康市| 赫章县| 慈利县| 江山市| 竹北市| 兴和县| 祁阳县| 五原县| 武穴市| 尼勒克县| 澎湖县| 青川县| 凌海市| 洛川县| 望城县|