Security 常用配置

以下是一些常用的配置方法及其用途:


1. permitAll()


•  用途:允许未认证的用户访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/public/**").permitAll()  // 公共路径允许所有用户访问
        .anyRequest().authenticated();  // 其他所有请求都需要认证
}

2. authenticated()


•  用途:要求用户必须经过认证才能访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/admin/**").authenticated()  // 管理员路径需要认证
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

3. hasRole()


•  用途:指定只有拥有特定角色的用户才能访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/admin/**").hasRole("ADMIN")  // 管理员路径需要ADMIN角色
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

4. hasAuthority()


•  用途:指定只有拥有特定权限的用户才能访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/admin/**").hasAuthority("ADMIN_ACCESS")  // 管理员路径需要ADMIN_ACCESS权限
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

5. access()


•  用途:使用Spring EL表达式来控制访问权限。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ADMIN') or hasRole('SUPER_ADMIN')")  // 管理员路径需要ADMIN或SUPER_ADMIN角色
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

6. denyAll()


•  用途:拒绝所有用户访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/secret/**").denyAll()  // 秘密路径拒绝所有人访问
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

7. anonymous()


•  用途:允许匿名用户访问指定的URL路径。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/public/**").anonymous()  // 公共路径允许匿名用户访问
        .anyRequest().authenticated();  // 其他所有请求需要认证
}

8. csrf()


•  用途:启用或禁用跨站请求伪造(CSRF)保护。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()  // 禁用CSRF保护
        .authorizeRequests()
        .anyRequest().permitAll();  // 其他所有请求不需要认证
}

9. formLogin()


•  用途:配置基于表单的登录页面。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/login").permitAll()  // 登录页面允许所有用户访问
        .anyRequest().authenticated()  // 其他所有请求需要认证
        .and()
        .formLogin()
        .loginPage("/login")  // 自定义登录页面
        .defaultSuccessUrl("/home", true)  // 成功后的默认跳转页面
        .failureUrl("/login?error")  // 登录失败后的跳转页面
        .permitAll();  // 访问登录页面不需要认证
}

10. logout()


•  用途:配置注销功能。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/logout").permitAll()  // 注销页面允许所有用户访问
        .anyRequest().authenticated()  // 其他所有请求需要认证
        .and()
        .logout()
        .logoutUrl("/logout")  // 自定义注销URL
        .logoutSuccessUrl("/login")  // 注销成功后的跳转页面
        .invalidateHttpSession(true)  // 注销时无效化HTTP会话
        .deleteCookies("JSESSIONID");  // 注销时删除JSESSIONID cookie
}

11. httpBasic()


•  用途:配置基本的身份验证。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/secure/**").authenticated()  // 安全路径需要认证
        .anyRequest().permitAll()  // 其他所有请求不需要认证
        .and()
        .httpBasic();  // 启用基本身份验证
}

12. sessionManagement()


•  用途:配置会话管理。
•  示例:
 

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .anyRequest().authenticated()  // 所有请求需要认证
        .and()
        .sessionManagement()
        .maximumSessions(1)  // 最多允许一个会话
        .expiredUrl("/login?expired")  // 会话过期后的跳转页面
        .maxSessionsPreventsLogin(false)  // 是否阻止新登录
        .expiredUrl("/login?expired")  // 会话过期后的跳转页面
        .invalidSessionUrl("/login?invalid")  // 无效会话后的跳转页面
        .sessionFixation().migrateSession();  // 会话固定保护
}

总结
通过上述配置方法,你可以灵活地控制应用程序的安全性,确保只有授权用户才能访问特定的资源。如果你有其他具体需求或需要进一步的配置示例,请随时告知!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值