2017-11-14 15 views
0

@RolesAllowedjdbcAuthentication()を使用しているときに機能しないようですが、inMemoryAuthentication()を使用しているとうまく動作します。 @RolesAllowedを削除すると、jdbcAuthentication()は正常に動作します。@RolesAlteredをconfigureGlobalSecurityと組み合わせて使用​​すると、

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(jsr250Enabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
     auth. 
      jdbcAuthentication() 
      .usersByUsernameQuery("select u.email, u.password, u.enabled from bagtag.user u where email=?") 
      .authoritiesByUsernameQuery("select u.email, r.name from bagtag.user u join bagtag.role r on (r.id = u.role_id) where u.email=?") 
      .dataSource(dataSource) 
      .passwordEncoder(bCryptPasswordEncoder); 
    } 

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

     http.csrf().disable().authorizeRequests().antMatchers("/api/**").fullyAuthenticated().and().httpBasic() 
       .realmName(REALM).authenticationEntryPoint(getBasicAuthEntryPoint()).and().sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS); 

    } 
} 

RequestMapping@RolesAllowedで行いますが、私は@RolesAllowedを削除する場合はそれが動作します。次のエラーが表示されます。私は@RolesAllowedを除去しようとし http.csrf().disable().authorizeRequests().antMatchers("/api/**").fullyAuthenticated().and().httpBasic()

に次の行を交換した

[email protected]: Username: [email protected]; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: SUPERADMIN

を返し System.out.println(SecurityContextHolder.getContext().getAuthentication().getPrincipal());

と認証されたユーザを検査することを試みた

{ 
    "timestamp": 1510628906600, 
    "status": 403, 
    "error": "Forbidden", 
    "exception": "org.springframework.security.access.AccessDeniedException", 
    "message": "Access is denied", 
    "path": "/api/user/admin" 
} 

@RestController 
@RequestMapping(value = "/api/user", produces = MediaType.APPLICATION_JSON_VALUE) 
public class UserController { 

    @Autowired 
    UserService us; 

    @RolesAllowed(Role.SUPERADMIN) 
    @RequestMapping(value = "/admin", method = RequestMethod.POST) 
    public ResponseEntity<User> createAdmin(@RequestBody UserWrapper uw) { 
     return new ResponseEntity<User>(uw.getUser(), HttpStatus.OK); 
    } 
} 

http.csrf().disable().authorizeRequests().antMatchers("/api/**").hasAnyRole(String.format("%s,%s,%s", Role.SUPERADMIN, Role.ADMIN, Role.STAFF)).and().httpBasic()

とこれはこれは私のデータベースの私の二つのテーブルである私にhttp status 403

{ 
    "timestamp": 1510629419006, 
    "status": 403, 
    "error": "Forbidden", 
    "message": "Access is denied", 
    "path": "/api/user/admin" 
} 

を与えます。ユーザーは1つのロールのみを持つことができます。

CREATE TABLE `role` (
    `role_id` int(11) NOT NULL AUTO_INCREMENT, 
    `role` varchar(255) DEFAULT NULL, 
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`role_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 

CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `email` varchar(255) NOT NULL, 
    `password` varchar(255) NOT NULL, 
    `first_name` varchar(255) NOT NULL, 
    `last_name` varchar(255) DEFAULT NULL, 
    `role_id` int(11) NOT NULL, 
    `enabled` bit(1) NOT NULL DEFAULT b'0', 
    `last_login` timestamp NULL DEFAULT NULL, 
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `email_UNIQUE` (`email`), 
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8 
+0

可能性のある重複した[SpringbootセキュリティhasRoleもが機能していない](https://stackoverflow.com/questions/41946473/springboot-security-hasrole-not-working) – dur

答えて

0

だから私は私の問題への解決策を見つけた、私は私のconfigureGlobalSecurity方法に.rolePrefix("ROLE_");を追加する必要がありました。

なぜこれが解決策であるかわかりません。 AuthenticationManagerBuilderの既定のロールプレフィックスは、ドキュメントに従って空のStringです。ですから、私の推測では、Springのデフォルトのロールプレフィックスは "ROLE_"です。

この狂気の背後にある本当の説明を誰かが知っている場合は、コメントまたは回答を投稿してください。私はとても感謝しています!

編集: @RolesAllowedデフォルトのプレフィックスはROLE_です。そのため、暫定セキュリティにプレフィックスROLE_を追加する必要があります。

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(jsr250Enabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
@Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
     auth. 
      jdbcAuthentication() 
      .usersByUsernameQuery(usersQuery) 
      .authoritiesByUsernameQuery(rolesQuery) 
      .dataSource(dataSource) 
      .passwordEncoder(bCryptPasswordEncoder) 
      //.rolePrefix fixed the problem 
      .rolePrefix("ROLE_");   
    } 

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

     http.csrf().disable().authorizeRequests().antMatchers("/api/**") 
      .hasAnyRole(String.format("%s,%s,%s", Role.SUPERADMIN, Role.ADMIN, Role.STAFF)).and().httpBasic() 
      .realmName(REALM).authenticationEntryPoint(getBasicAuthEntryPoint()).and().sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS); 

    } 
} 
+1

問題ではありません*デフォルトのロールプレフィックスのは、 AuthenticationManagerBuilderは、ドキュメントに従って*。空のStringです。問題はデフォルトの@RolesAllowedです。 – dur

関連する問題