【问题标题】:How to provide access (Spring Security) [closed]如何提供访问权限(Spring Security)[关闭]
【发布时间】:2019-10-24 13:27:50
【问题描述】:

如何设置权限,以便只有管理员可以使用他的用户名和密码访问 JSP 页面。假设一个页面 (allStudents.jsp) 仅对管理员可用,为此他必须输入他的用户名和密码

如何设置权限,以便只有管理员可以使用他的用户名和密码访问 JSP 页面。假设一个页面 (allStudents.jsp) 仅对管理员可用,为此他必须输入他的用户名和密码

package adil.java.schoolmaven.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.stereotype.Component;

@Order(1)
@Configuration
@EnableWebSecurity
@Component
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyBasicAuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("admin").password(passwordEncoder().encode("1234"))
          .authorities("ROLE_ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/securityNone").permitAll() //??????
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .authenticationEntryPoint(authenticationEntryPoint);

        http.addFilterAfter(new CustomFilter(),
          BasicAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

enter image description here

我已更改代码请查看此代码

@Order(1)
@Configuration
@EnableWebSecurity
@Component
public class СostumWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyBasicAuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("admin").password(passwordEncoder().encode("1234"))
          .authorities("ROLE_ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/allStudents").hasRole("ADMIN");
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .authenticationEntryPoint(authenticationEntryPoint);

        http.addFilterAfter(new CustomFilter(),
          BasicAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

【问题讨论】:

  • 调用 allStudents.jsp 的端点是什么?你能把你的控制器贴在你调用 allStudents.jsp 的地方吗?

标签: java spring spring-mvc jakarta-ee spring-security


【解决方案1】:

整页授权示例

为 http 元素启用表达式后,可以使用 URL 模式 保护如下:

@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeRequests()
          .antMatchers("/allStudents").hasRole("ROLE_ADMIN"); // assuming this is your endpoint/controller for allStudents.jsp page
    }
}

方法级授权示例——@PreAuthorize

安全表达式可用于保护业务功能 方法级别也是如此,通过使用注释。

注解@PreAuthorize 和@PostAuthorize(以及 @PreFilter 和 @PostFilter) 支持 Spring 表达式语言 (SpEL) 并提供基于表达式的访问控制。

首先,为了使用方法级别的安全性,我们需要启用这个 在使用@EnableGlobalMethodSecurity 的安全配置中:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   ...
}

然后,我们可以使用 Spring @PreAuthorize 注解来保护方法:

@Service
public class FooService {
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public List<Foo> findAll() { ... }
    ...
}

对于这个例子更深入的解释,可以参考这个link

【讨论】:

  • .hasRole("ADMIN") 更改为.hasRole("ROLE_ADMIN"),因为这是您在内存身份验证用户中设置的角色。
猜你喜欢
  • 2019-01-09
  • 1970-01-01
  • 2015-07-14
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 2018-05-31
相关资源
最近更新 更多