Exception类或其子类的实例有?
时间: 2024-06-03 12:01:09 浏览: 9
Exception类或其子类的实例有:ArithmeticException、ArrayIndexOutOfBoundsException、ClassNotFoundException、FileNotFoundException、IOException、InterruptedException、NullPointerException、NumberFormatException、RuntimeException、SQLException等。
相关问题
常见的异常类有哪些?
常见的异常类有以下几种:
1. `Exception`:所有异常类的基类,用于捕获所有类型的异常。
2. `ArithmeticException`:在数值计算过程中发生的异常,例如除零错误。
3. `NullPointerException`:当应用程序试图在需要对象的地方使用 null 时引发的异常。
4. `ArrayIndexOutOfBoundsException`:当应用程序试图访问数组中不存在的元素时引发的异常。
5. `ClassCastException`:当试图将对象强制转换为不是实例化类或其子类的类时引发的异常。
6. `FileNotFoundException`:当试图打开指定路径下不存在的文件时引发的异常。
7. `IOException`:在输入/输出操作失败或中断时引发的异常。
8. `NumberFormatException`:当字符串转换为数字时发生格式错误时引发的异常。
可以给我我一段代码实例吗?
当然可以,以下是一个使用多个 `WebSecurityConfigurerAdapter` 子类实现不同安全策略的示例代码:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// 配置基于内存的身份验证
auth.inMemoryAuthentication()
.withUser("user").password("{noop}password").roles("USER")
.and()
.withUser("admin").password("{noop}password").roles("ADMIN");
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置 API 访问的安全规则
http.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.httpBasic();
}
}
@Configuration
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 配置基于表单的身份验证
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic().disable();
}
}
}
```
这个示例代码中,我们定义了两个 `WebSecurityConfigurerAdapter` 的子类,分别用于实现不同的安全策略。其中,`ApiWebSecurityConfigurationAdapter` 子类用于配置 API 访问的安全规则,要求 `/api/**` 的请求必须具备 `USER` 角色才能访问,而 `FormLoginWebSecurityConfigurerAdapter` 子类则用于配置基于表单的身份验证,要求所有请求都必须经过身份验证才能访问。通过在子类中使用 `@Order` 注解来指定执行顺序,我们可以确保安全规则能够正确地匹配请求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)