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

java中Spring Security的實例詳解

 更新時間:2017年09月05日 10:22:55   投稿:lqh  
這篇文章主要介紹了java中Spring Security的實例詳解的相關(guān)資料,spring security是一個多方面的安全認證框架,提供了基于JavaEE規(guī)范的完整的安全認證解決方案,需要的朋友可以參考下

java中Spring Security的實例詳解

spring security是一個多方面的安全認證框架,提供了基于JavaEE規(guī)范的完整的安全認證解決方案。并且可以很好與目前主流的認證框架(如CAS,中央授權(quán)系統(tǒng))集成。使用spring security的初衷是解決不同用戶登錄不同應(yīng)用程序的權(quán)限問題,說到權(quán)限包括兩部分:認證和授權(quán)。認證是告訴系統(tǒng)你是誰,授權(quán)是指知道你是誰后是否有權(quán)限訪問系統(tǒng)(授權(quán)后一般會在服務(wù)端創(chuàng)建一個token,之后用這個token進行后續(xù)行為的交互)。

spring security提供了多種認證模式,很多第三方的認證技術(shù)都可以很好集成:

  • Form-based authentication (用于簡單的用戶界面)
  • OpenID 認證
  • Authentication based on pre-established request headers (such as Computer - Associates Siteminder)根據(jù)預(yù)先建立的請求頭進行驗證
  • JA-SIG Central Authentication Service ( CAS, 一個開源的SSO系統(tǒng))
  • Java Authentication and Authorization Service (JAAS)

這里只列舉了部分,后面會重點介紹如何集成CAS,搭建自己的認證服務(wù)。

在spring boot項目中使用spring security很容易,這里介紹如何基于內(nèi)存中的用戶和基于數(shù)據(jù)庫進行認證。

準(zhǔn)備

pom依賴:

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

配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Bean
  public UserDetailsService userDetailsService() {
    return new CustomUserDetailsService();
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("rhwayfun").password("1209").roles("USERS")
        .and().withUser("admin").password("123456").roles("ADMIN");
    //auth.jdbcAuthentication().dataSource(securityDataSource);
    //auth.userDetailsService(userDetailsService());
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()//配置安全策略
        //.antMatchers("/","/index").permitAll()//定義/請求不需要驗證
        .anyRequest().authenticated()//其余的所有請求都需要驗證
        .and()
        .formLogin()
        .loginPage("/login")
        .defaultSuccessUrl("/index")
        .permitAll()
        .and()
        .logout()
        .logoutSuccessUrl("/login")
        .permitAll();//定義logout不需要驗證

    http.csrf().disable();
  }

}

這里需要覆蓋WebSecurityConfigurerAdapter的兩個方法,分別定義什么請求需要什么權(quán)限,并且認證的用戶密碼分別是什么。

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
  /**
   * 統(tǒng)一注冊純RequestMapping跳轉(zhuǎn)View的Controller
   */
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("/login");
  }
}

添加登錄跳轉(zhuǎn)的URL,如果不加這個配置也會默認跳轉(zhuǎn)到/login下,所以這里還可以自定義登錄的請求路徑。

登錄頁面:

<!DOCTYPE html>

<%--<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>--%>

<html>
<head>
  <meta charset="utf-8">
  <title>Welcome SpringBoot</title>
  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/index.js"></script>
</head>
<body>

<form name="f" action="/login" method="post">
  <input id="name" name="username" type="text"/><br>
  <input id="password" name="password" type="password"><br>
  <input type="submit" value="login">
  <input name="_csrf" type="hidden" value="${_csrf}"/>
</form>

<p id="users">

</p>

<script>
  $(function () {
    $('[name=f]').focus()
  })
</script>
</body>

</html>

基于內(nèi)存

SecurityConfig這個配置已經(jīng)是基于了內(nèi)存中的用戶進行認證的,

auth.inMemoryAuthentication()//基于內(nèi)存進行認證
.withUser("rhwayfun").password("1209")//用戶名密碼
.roles("USERS")//USER角色
        .and()
        .withUser("admin").password("123456").roles("ADMIN");//ADMIN角色

訪問首頁會跳轉(zhuǎn)到登錄頁面這成功,使用配置的用戶登錄會跳轉(zhuǎn)到首頁。

基于數(shù)據(jù)庫

基于數(shù)據(jù)庫會復(fù)雜一些,不過原理是一致的只不過數(shù)據(jù)源從內(nèi)存轉(zhuǎn)到了數(shù)據(jù)庫。從基于內(nèi)存的例子我們大概知道spring security認證的過程:從內(nèi)存查找username為輸入值得用戶,如果存在著驗證其角色時候匹配,比如普通用戶不能訪問admin頁面,這點可以在controller層使用@PreAuthorize("hasRole('ROLE_ADMIN')")實現(xiàn),表示只有admin角色的用戶才能訪問該頁面,spring security中角色的定義都是以ROLE_開頭,后面跟上具體的角色名稱。

如果要基于數(shù)據(jù)庫,可以直接指定數(shù)據(jù)源即可:

auth.jdbcAuthentication().dataSource(securityDataSource);

只不過數(shù)據(jù)庫標(biāo)是spring默認的,包括三張表:users(用戶信息表)、authorities(用戶角色信息表)

以下是查詢用戶信息以及創(chuàng)建用戶角色的SQL(部分,詳細可到JdbcUserDetailsManager類查看):

public static final String DEF_CREATE_USER_SQL = "insert into users (username, password, enabled) values (?,?,?)";

public static final String DEF_INSERT_AUTHORITY_SQL = "insert into authorities (username, authority) values (?,?)";

public static final String DEF_USER_EXISTS_SQL = "select username from users where username = ?";

那么,如果要自定義數(shù)據(jù)庫表這需要配置如下,并且實現(xiàn)UserDetailsService接口:

auth.userDetailsService(userDetailsService());

@Bean
  public UserDetailsService userDetailsService() {
    return new CustomUserDetailsService();
  }

CustomUserDetailsService實現(xiàn)如下:

@Service
public class CustomUserDetailsService implements UserDetailsService {

  /** Logger */
  private static Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);

  @Resource
  private UserRepository userRepository;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    if (StringUtils.isBlank(username)) {
      throw new IllegalArgumentException("username can't be null!");
    }
    User user;
    try {
      user = userRepository.findByUserName(username);
    } catch (Exception e) {
      log.error("讀取用戶信息異常,", e);
      return null;
    }
    if (user == null) {
      return null;
    }
    List<UserAuthority> roles = userRepository.findRoles(user.getUserId());
    List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    for (UserAuthority role : roles) {
      SimpleGrantedAuthority authority = new SimpleGrantedAuthority(String.valueOf(role.getAuthId()));
      authorities.add(authority);
    }
    return new org.springframework.security.core.userdetails.User(username, "1234", authorities);
  }


}

我們需要實現(xiàn)loadUserByUsername方法,這里面其實級做了兩件事:查詢用戶信息并返回該用戶的角色信息。

數(shù)據(jù)庫設(shè)計如下:

數(shù)據(jù)庫設(shè)計

g_users:用戶基本信息表
g_authority:角色信息表
r_auth_user:用戶角色信息表,這里沒有使用外鍵約束。

使用mybatis generator生成mapper后,創(chuàng)建數(shù)據(jù)源SecurityDataSource。

@Configuration
@ConfigurationProperties(prefix = "security.datasource")
@MapperScan(basePackages = DataSourceConstants.MAPPER_SECURITY_PACKAGE, sqlSessionFactoryRef = "securitySqlSessionFactory")
public class SecurityDataSourceConfig {

  private String url;

  private String username;

  private String password;

  private String driverClassName;

  @Bean(name = "securityDataSource")
  public DataSource securityDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
  }

  @Bean(name = "securityTransactionManager")
  public DataSourceTransactionManager securityTransactionManager() {
    return new DataSourceTransactionManager(securityDataSource());
  }

  @Bean(name = "securitySqlSessionFactory")
  public SqlSessionFactory securitySqlSessionFactory(@Qualifier("securityDataSource") DataSource securityDataSource)
      throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(securityDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources(DataSourceConstants.MAPPER_SECURITY_LOCATION));
    return sessionFactory.getObject();
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  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;
  }

  public String getDriverClassName() {
    return driverClassName;
  }

  public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
  }
}

那么DAO UserRepository就很好實現(xiàn)了:

public interface UserRepository{

  User findByUserName(String username);

  List<UserAuthority> findRoles(int userId);

}

在數(shù)據(jù)庫插入相關(guān)數(shù)據(jù),重啟項目。仍然訪問首頁跳轉(zhuǎn)到登錄頁后輸入數(shù)據(jù)庫插入的用戶信息,如果成功跳轉(zhuǎn)到首頁這說明認證成功。

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Java基礎(chǔ)之Web服務(wù)器與Http詳解

    Java基礎(chǔ)之Web服務(wù)器與Http詳解

    無論你是前端開發(fā)者還是后端開發(fā)者,以及測試工程師,這篇文章的知識都是你需要弄懂的。讀完這一篇文章,將全面弄懂 HTTP 協(xié)議、TCP 協(xié)議,面試官再也難不倒你相關(guān)知識
    2021-09-09
  • MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    MyBatis插入Insert、InsertSelective的區(qū)別及使用心得

    這篇文章主要介紹了MyBatis插入Insert、InsertSelective的區(qū)別及使用心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java異常鏈表throw結(jié)構(gòu)assert詳細解讀

    Java異常鏈表throw結(jié)構(gòu)assert詳細解讀

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Java設(shè)計模式之單例模式Singleton Pattern詳解

    Java設(shè)計模式之單例模式Singleton Pattern詳解

    這篇文章主要介紹了Java設(shè)計模式之單例模式Singleton Pattern詳解,一些常用的工具類、線程池、緩存,數(shù)據(jù)庫,數(shù)據(jù)庫連接池、賬戶登錄系統(tǒng)、配置文件等程序中可能只允許我們創(chuàng)建一個對象,這就需要單例模式,需要的朋友可以參考下
    2023-12-12
  • Kotlin 基礎(chǔ)語法詳細介紹

    Kotlin 基礎(chǔ)語法詳細介紹

    這篇文章主要介紹了Kotlin 基礎(chǔ)語法詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • java中快速創(chuàng)建帶初始值的List和Map實例

    java中快速創(chuàng)建帶初始值的List和Map實例

    下面小編就為大家?guī)硪黄猨ava中快速創(chuàng)建帶初始值的List和Map實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Idea安裝bpmn插件actiBPM的詳細過程(解決高版本無法安裝actiBPM插件)

    Idea安裝bpmn插件actiBPM的詳細過程(解決高版本無法安裝actiBPM插件)

    這篇文章主要介紹了Idea安裝bpmn插件actiBPM的詳細過程(解決高版本無法安裝actiBPM插件)的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • Java實現(xiàn)字符串匹配的示例代碼

    Java實現(xiàn)字符串匹配的示例代碼

    這篇文章主要介紹了Java實現(xiàn)字符串匹配,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • spring/springboot整合curator遇到的坑及解決

    spring/springboot整合curator遇到的坑及解決

    這篇文章主要介紹了spring/springboot整合curator遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 使用Spring動態(tài)修改bean屬性的key

    使用Spring動態(tài)修改bean屬性的key

    這篇文章主要介紹了使用Spring動態(tài)修改bean屬性的key方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論