【问题标题】:Don't know how configure access in spring boot security [duplicate]不知道如何在 Spring Boot 安全中配置访问 [重复]
【发布时间】:2017-05-29 01:45:13
【问题描述】:

编辑 5

愚蠢的我,ADMIN 没有被识别,因为我写了 authorityByUsernameQuery 查询错误:所以如果你有同样的问题,请检查一下。 现在配置工作:这里以代码为例

    protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/admin", "/admin/user").access("hasRole('ADMIN')") /**only ADMIN can see those pages**/
    .antMatchers("/page*").access("hasRole('USER')") /**only the users can see all the pages that start with 'page'**/
    .anyRequest().permitAll() /**all the other pages can seen by anyone**/
    .and()
    .formLogin()
    .loginPage("/login")
    .permitAll()
    .and()
    .exceptionHandling().accessDeniedPage("/403")
    .and()
    .logout().logoutSuccessUrl("/login?logout")
    .and()
    .csrf();
}

编辑 4

如果我写了

${pageContext.request.isUserInRole('ADMIN')}

${pageContext.request.isUserInRole('USER')}

在我的 jsp 中,它显示“false”,因此它无法识别角色。但是为什么呢?


编辑 3

好的,现在我有一个新问题(加上前一个问题):我怎样才能允许每个人访问某些页面?否则没有用户可以订阅。


编辑 2

根据建议,我再次编辑了代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/admin", "/admin/users").access("hasRole('ADMIN')") 
    .anyRequest().authenticated()
    .and()
    .formLogin()
    .loginPage("/login")
    .permitAll()
    .and()
    .exceptionHandling().accessDeniedPage("/403")
    .and()
    .logout().logoutSuccessUrl("/login?logout")
    .and()
    .csrf();
}

但它仍然无法正常工作(管理员无法识别并且无法访问他的页面)。所以,我认为这不仅仅是 RULE_ADMIN 的问题。

在我的数据库(postgres)中,我创建了这样的表:

CREATE TABLE public.utenti
(
  username character varying(45) NOT NULL,
  password character varying(45) NOT NULL,
  abilitazione boolean NOT NULL DEFAULT true,
  email character varying(45) NOT NULL,
  CONSTRAINT username PRIMARY KEY (username)
)

CREATE TABLE public.ruoli_utente
(
  user_role_id integer NOT NULL DEFAULT nextval('ruoli_utente_user_role_id_seq'::regclass),
  username character varying(45) NOT NULL,
  ruolo character varying(45) NOT NULL,
  CONSTRAINT user_role_pk PRIMARY KEY (user_role_id),
  CONSTRAINT username_fk FOREIGN KEY (username)
      REFERENCES public.utenti (username) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

authoritiesByUsernameQuery 方法是否可能因为调用方式不同而无法识别角色列?如果是这样,为什么 usersByUsernameQuery 会识别启用的用户,因为我将列称为“abilitazione”而不是“启用”?


编辑

也许我开始理解了,我已经像这样改变了我的方法:

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
        .authorizeRequests()
            .antMatchers("/hello").access("hasRole('ROLE_ADMIN')")      
            .anyRequest().permitAll()
            .and()
        .formLogin()
            .loginPage("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
        .logout()
            .logoutSuccessUrl("/login?logout")  
            .and()
        .exceptionHandling()
            .accessDeniedPage("/403")
            .and()
        .csrf();
}

但它无法识别管理员用户。


我正在关注 this guide 以在我的 Spring Boot Web 应用程序中添加安全性。 我的安全类中的 configure 方法有问题,我很难理解如何在网上搜索解决它。

如果用户通过身份验证,我想要做的是允许所有页面的访问,但管理页面除外。

这是我写的:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/adminPage").access("hasRole('ROLE_ADMIN')");  
    http
        .authorizeRequests()
            .anyRequest().authenticated();
    http
        .formLogin()
            .loginPage("/login")
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
        .logout()
            .logoutSuccessUrl("/login?logout") 
            .and()
        .exceptionHandling()
            .accessDeniedPage("/403")
            .and()
        .csrf();
}

但它不起作用,我不明白为什么/该怎么做,我也没有在网上找到解决方案(或者它不起作用/我不明白)。

【问题讨论】:

    标签: java spring-boot spring-security


    【解决方案1】:

    我愿意:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
        .antMatcher("/**").access("hasRole('ROLE_USER')")
        .antMatchers("/adminPage").access("hasRole('ROLE_ADMIN')")
        ...;
    }
    

    然后以某种方式编写您自己的 AutenticationManager : Custom Authentication Manager with Spring Security and Java Configuration

    并创建您自己的对象用户 extends org.springframework.security.core.userdetails.User 和适当的 username, password, new ArrayList<GrantedAuthority>{{ add(new SimpleGrantedAuthority("ROLE_USER")}} (如果用户是管理员,则add(new SimplaGrantedAuthority("ROLE_ADMIN")))。

    您可能还必须实现 UserDetailsS​​ervice。

    【讨论】:

    • 感谢您的建议,但我想尽可能避免这种方法,以保持项目简单。
    猜你喜欢
    • 2014-02-05
    • 2021-02-21
    • 1970-01-01
    • 2019-09-29
    • 2021-11-23
    • 2019-08-25
    • 2021-10-19
    • 2019-05-02
    • 2019-09-23
    相关资源
    最近更新 更多