欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot整合Spring Security的示例代碼

 更新時(shí)間:2018年04月08日 10:25:13   作者:u283056051  
這篇文章主要介紹了Spring Boot整合Spring Security的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文講述Spring Boot整合Spring Security在方法上使用注解實(shí)現(xiàn)權(quán)限控制,使用自定義UserDetailService,從MySQL中加載用戶信息。使用Security自帶的MD5加密,對(duì)用戶密碼進(jìn)行加密。頁(yè)面模板采用thymeleaf引擎。

源碼地址:https://github.com/li5454yong/springboot-security.git

1、引入pom依賴

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.4.RELEASE</version>
 </parent>

 <dependencies>
  <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>
  <dependency>
   <groupId>org.springframework.security.oauth</groupId>
   <artifactId>spring-security-oauth2</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.34</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.15</version>
  </dependency>
 </dependencies>

這里使用druid連接池,Spring Data Jpa實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問(wèn)。

2、配置Spring Security

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//開(kāi)啟security注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 @Bean
 @Override
 protected AuthenticationManager authenticationManager() throws Exception {
  return super.authenticationManager();
 }

 @Override
 protected void configure(HttpSecurity http) throws Exception {
  //允許所有用戶訪問(wèn)"/"和"/home"
  http.authorizeRequests()
    .antMatchers("/", "/home").permitAll()
    //其他地址的訪問(wèn)均需驗(yàn)證權(quán)限
    .anyRequest().authenticated()
    .and()
    .formLogin()
    //指定登錄頁(yè)是"/login"
    .loginPage("/login")
    .defaultSuccessUrl("/hello")//登錄成功后默認(rèn)跳轉(zhuǎn)到"/hello"
    .permitAll()
    .and()
    .logout()
    .logoutSuccessUrl("/home")//退出登錄后的默認(rèn)url是"/home"
    .permitAll();
 }

 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth
   .userDetailsService(customUserDetailsService())
   .passwordEncoder(passwordEncoder());

 }

 /**
  * 設(shè)置用戶密碼的加密方式為MD5加密
  * @return
  */
 @Bean
 public Md5PasswordEncoder passwordEncoder() {
  return new Md5PasswordEncoder();
 }

 /**
  * 自定義UserDetailsService,從數(shù)據(jù)庫(kù)中讀取用戶信息
  * @return
  */
 @Bean
 public CustomUserDetailsService customUserDetailsService(){
  return new CustomUserDetailsService();
 }
}

這里只做了基本的配置,設(shè)置了登錄url、登錄成功后跳轉(zhuǎn)的url、退出后跳轉(zhuǎn)到的url。使用@EnableGlobalMethodSecurity(prePostEnabled = true)這個(gè)注解,可以開(kāi)啟security的注解,我們可以在需要控制權(quán)限的方法上面使用@PreAuthorize,@PreFilter這些注解。

3、自定義userDetailService

public class CustomUserDetailsService implements UserDetailsService {
 @Autowired //數(shù)據(jù)庫(kù)服務(wù)類
 private SUserService suserService;

 @Override
 public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  //SUser對(duì)應(yīng)數(shù)據(jù)庫(kù)中的用戶表,是最終存儲(chǔ)用戶和密碼的表,可自定義
  //本例使用SUser中的email作為用戶名:
  SUser user = suserService.findUserByEmail(userName); 

  if (user == null) {

   throw new UsernameNotFoundException("UserName " + userName + " not found");

  }

  // SecurityUser實(shí)現(xiàn)UserDetails并將SUser的Email映射為username
  SecurityUser securityUser = new SecurityUser(user);
  Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
  authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  return securityUser; 

 }

}

這里只需要實(shí)現(xiàn)UserDetailsService 接口,重寫loadUserByUsername方法,從數(shù)據(jù)庫(kù)中取出用戶信息。最后返回一個(gè)UserDetails 實(shí)現(xiàn)類。

4、定義錯(cuò)誤處理配置

@Configuration
public class ErrorPageConfig {
 @Bean
 public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
  return new MyCustomizer();
 }
 private static class MyCustomizer implements EmbeddedServletContainerCustomizer {
  @Override
  public void customize(ConfigurableEmbeddedServletContainer container) {
   container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));
  }
 }
}

訪問(wèn)發(fā)生錯(cuò)誤時(shí),跳轉(zhuǎn)到”/403”.

5、Controller接口

@Controller
public class IndexController {

 @Resource
 private SUserService sUserService;

 @RequestMapping("/home")
 public String home() {
  return "home";

 }


 @PreAuthorize("hasRole('user')")
 @RequestMapping(value = "/admin",method = RequestMethod.GET)
 public String toAdmin(){

  return "helloAdmin";
 }

 @RequestMapping("/hello")
 public String hello() {

  return "hello";

 }

 @RequestMapping("/login")
 public String login(){
  return "login";
 }

 @RequestMapping("/")
 public String root() {
  return "index";

 }

 @RequestMapping("/403")
 public String error(){
  return "403";
 }
}

在toAdmin()方法上面使用了@PreAuthorize(“hasRole(‘user')”),表示訪問(wèn)這個(gè)方法需要擁有user角色。如果希望控制到權(quán)限層面,可以使用@PreAuthorize(“hasPermission()”)。這里只是用了其中的一個(gè)用法,更多的使用方法可以去看官方文檔。需要注意的是,Spring Security默認(rèn)的角色前綴是”ROLE_”,使用hasRole方法時(shí)已經(jīng)默認(rèn)加上了,因此我們?cè)跀?shù)據(jù)庫(kù)里面的用戶角色應(yīng)該是“ROLE_user”,在user前面加上”ROLE_”前綴。

6、測(cè)試

啟動(dòng)項(xiàng)目,訪問(wèn)http://localhost:1130/login

點(diǎn)擊登錄后進(jìn)入到“/hello”

點(diǎn)擊跳轉(zhuǎn)到管理員頁(yè)面

在后臺(tái)的“/admin”這個(gè)url對(duì)應(yīng)的方法上面,限制了用戶必須要擁有“user”角色。在數(shù)據(jù)庫(kù)中也設(shè)置了登錄的用戶有這個(gè)角色。
現(xiàn)在我們修改數(shù)據(jù)庫(kù)中的用戶角色,改為“ROLE_admin”。退出登錄后重新登錄,再次點(diǎn)擊“前往管理員頁(yè)面”按鈕,會(huì)跳轉(zhuǎn)到如下頁(yè)面。

 

因?yàn)楝F(xiàn)在沒(méi)有了“user”權(quán)限,所以訪問(wèn)的時(shí)候拋出了異常,被攔截后重定向到了“/403”。

7、POST方式訪問(wèn),錯(cuò)誤碼403

首先,把“/admin”改為POST請(qǐng)求

 @PreAuthorize("hasRole('user')")
 @RequestMapping(value = "/admin",method = RequestMethod.POST)
 public String toAdmin(){
  return "helloAdmin";
 }

把“前往管理員頁(yè)面”按鈕的請(qǐng)求方式從原來(lái)的form表達(dá)get提交,改為ajax方式POST提交。至于為什么不是用form的POST提交后面再講。先修改代碼

<body>

<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>

<!--<form th:action="@{/logout}" method="post">

 <input type="submit" value="Sign Out"/>
</form>
<form th:action="@{/admin}" method="get">
 <input th:type="submit" th:value="前往管理員頁(yè)面"/>

</form>-->
<a th:href="@{/admin}" rel="external nofollow" >前往管理員用戶頁(yè)面</a>
<input th:type="submit" onclick="testPost()" th:value="前往管理員頁(yè)面"/>
</body>
<script>
 function testPost() {
  $.ajax({
   url:"/admin",
   type:'POST',

   success:function (data) {

   }
  });
 }
</script>

點(diǎn)擊“前往管理員頁(yè)面”按鈕,在調(diào)試臺(tái)可以看到如下

 

這是因?yàn)榭蚣軆?nèi)部防止CSRF(Cross-site request forgery跨站請(qǐng)求偽造)的發(fā)生,限制了除了get以外的大多數(shù)方法。

下面說(shuō)解決辦法:

首先在標(biāo)簽內(nèi)添加如下內(nèi)容。

 <meta name="_csrf" th:content="${_csrf.token}"/>
 <meta name="_csrf_hader" th:content="${_csrf.headerName}"/>

只要添加這個(gè)token,后臺(tái)就會(huì)驗(yàn)證這個(gè)token的正確性,如果正確,則接受post訪問(wèn)。
然后在ajax代碼中添加以下代碼:

var token = $('meta[name="_csrf"]').attr("content");
var header = $('meta[name="_csrf_hader"]').attr("content");
$(document).ajaxSend(function(e,xhr,opt){
   xhr.setRequestHeader(header,token);
  });

這樣就可以正常使用POST、DELETE等其他方式來(lái)訪問(wèn)了。

上面說(shuō)到使用表單的POST方式來(lái)提交,通過(guò)查看頁(yè)面源代碼可以看到

 

框架在form表單中自動(dòng)插入了一個(gè)隱藏域,value值就是那個(gè)token,所以使用form表單來(lái)提交POST請(qǐng)求是可以直接通過(guò)的,而ajax方式提交的話,需要加上那段代碼。

好了,這篇文章就講到這,后面還會(huì)有文章講述REST API風(fēng)格如何來(lái)使用Spring Security來(lái)控制權(quán)限。

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

相關(guān)文章

  • SpringAOP切入點(diǎn)規(guī)范及獲取方法參數(shù)的實(shí)現(xiàn)

    SpringAOP切入點(diǎn)規(guī)范及獲取方法參數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了SpringAOP切入點(diǎn)規(guī)范及獲取方法參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺談SpringBoot @Autowired的兩種注入方式

    淺談SpringBoot @Autowired的兩種注入方式

    本文主要介紹了兩種SpringBoot @Autowired注入方式,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Java集合中的Iterator迭代器接口詳解

    Java集合中的Iterator迭代器接口詳解

    這篇文章主要介紹了Java集合中的Iterator迭代器接口詳解,Iterator,被稱為迭代器接口,本身并不提供存儲(chǔ)對(duì)象的能力,主要用于遍歷Collection中的元素,需要的朋友可以參考下
    2023-11-11
  • Failed to execute goal org...的解決辦法

    Failed to execute goal org...的解決辦法

    這篇文章主要介紹了Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java判斷閏年的2種方法示例

    Java判斷閏年的2種方法示例

    這篇文章主要給大家介紹了關(guān)于Java判斷閏年的2種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • java二維數(shù)組實(shí)現(xiàn)推箱子小游戲

    java二維數(shù)組實(shí)現(xiàn)推箱子小游戲

    這篇文章主要為大家詳細(xì)介紹了java二維數(shù)組實(shí)現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java Boolean 初始化方式詳解

    Java Boolean 初始化方式詳解

    這篇文章主要介紹了Java Boolean 初始化方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 多線程下嵌套異步任務(wù)導(dǎo)致程序假死問(wèn)題

    多線程下嵌套異步任務(wù)導(dǎo)致程序假死問(wèn)題

    這篇文章主要介紹了多線程下嵌套異步任務(wù)導(dǎo)致程序假死問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SecurityUtils.getSubject().getPrincipal()為null的問(wèn)題

    SecurityUtils.getSubject().getPrincipal()為null的問(wèn)題

    這篇文章主要介紹了SecurityUtils.getSubject().getPrincipal()為null的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java實(shí)現(xiàn)線程按序交替執(zhí)行的方法詳解

    Java實(shí)現(xiàn)線程按序交替執(zhí)行的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)線程按序交替執(zhí)行,文中的示例代碼講解詳細(xì),對(duì)我們了解線程有一定幫助,需要的可以參考一下
    2022-10-10

最新評(píng)論