天天看點

【開發技術】SpringSecurity如何在開發中結合自定義使用者權限?

作者:MONE00G互聯
【開發技術】SpringSecurity如何在開發中結合自定義使用者權限?

Spring Security 是一個強大而靈活的企業級認證和通路控制架構,它為 Web 應用提供了廣泛的安全功能,包括身份驗證、授權和攻擊預防等功能。Spring Security 是基于 Spring 架構建構的,由 Spring 社群維護。

Spring Security 的優點和缺點

優點:

- 提供可插拔的安全架構,用于建構 Web 應用程式,具有身份驗證和授權的支援。

- 提供 Spring IOC 容器,友善執行個體化和管理安全服務。

- 支援 XML 和基于 Java 的配置方法。

- 支援有狀态和無狀态身份驗證機制。

- 與 Spring Boot 和其他流行的架構容易內建。

缺點:

- 與其他架構,如 Apache Shiro 相比,存在略高的學習曲線。

- 對于某些場景,如自定義的 SSO,需要手動實作,因為沒有直接的支援。

【開發技術】SpringSecurity如何在開發中結合自定義使用者權限?

在項目中如何應用 Spring Security 并結合自定義使用者權限?

Spring Security 可以用于任何 Web 應用程式項目,無論大小。以下是一個在 Spring Boot 應用程式中實作 Spring Security 的示例。

1. 首先,在 pom.xml 檔案中添加 Spring Security 依賴項:

<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-web</artifactId>
	<version>${spring-security.version}</version>
</dependency>           

2. 建立一個配置類,擴充 WebSecurityConfigurerAdapter 并重寫 configure 方法以定義安全政策。下面是一個簡單的示例:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	UserService userService;
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
		.authorizeRequests()
		.antMatchers("/admin/**").hasRole("ADMIN")
		.antMatchers("/user/**").hasRole("USER")
		.anyRequest().permitAll()
		.and()
		.formLogin()
		.and()
		.httpBasic();
	}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		auth
		.userDetailsService(userService)
		.passwordEncoder(passwordEncoder());
	}
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
}           

在這個配置中,我們建立了一個安全政策,要求使用者具有 ADMIN 角色才能通路與 /admin/** 比對的 URL,要求使用者具有 USER 角色才能通路與 /user/** 比對的 URL,而對于其他所有 URL,允許任何經過身份驗證的使用者通路。此外,我們配置了一個使用者詳細資訊服務,并使用 BCryptPasswordEncoder 對密碼進行編碼。

3. 在 UserService 中,我們實作了 UserDetailsService 接口,并重寫了 loadUserByUsername 方法來擷取使用者詳細資訊。

@Service
public class UserServiceImpl implements UserDetailsService {
	@Autowired
	UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
	User user = userRepository.findByUsername(username);
	if (user == null) {
		throw new UsernameNotFoundException("User not found: " + username);
	}
	List<GrantedAuthority> authorities = new ArrayList<>();
	for (Role role : user.getRoles()) {
		authorities.add(new SimpleGrantedAuthority(role.getName()));
	}
	return new org.springframework.security.core.userdetails.User(
	user.getUsername(), user.getPassword(), authorities);
	}
}           

在這個例子中,我們從 UserRepository 中擷取使用者詳細資訊,并将使用者的角色作為 GrantedAuthority 清單添加到 UserDetails 中。

4. 在自定義的 UserRepository 中,我們定義了一個方法 findByUsername,用于擷取使用者名比對的使用者資訊。

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
	User findByUsername(String username);
}
           

通過上述代碼,我們可以将 Spring Security 應用于項目中并結合自定義使用者權限實作安全控制。

繼續閱讀