【问题标题】:NullPointerException when i use @Secured in my RestController [duplicate]当我在我的 RestController 中使用 @Secured 时出现 NullPointerException [重复]
【发布时间】:2017-04-18 11:00:22
【问题描述】:

我是 Spring Security 的初学者,所以我用两种方法编写了一个控制器:

@RestController
public class EtudiantRestService {

    @Autowired
    private EtudiantReository etudiantReository;

    @Secured(value = {"ROLE_ADMIN","ROLE_SCOLARITE"})
    @RequestMapping(value = "/saveEtudiant",method = RequestMethod.GET)
    private Etudiant saveEtudiant(Etudiant etudiant){
        System.out.println(etudiant.getNom());
        System.out.println(etudiant.getPrenom());
        return etudiantReository.save(etudiant);
    }

    @Secured(value = {"ROLE_ADMIN","ROLE_SCOLARITE","ROLE_PROF","ROLE_ETUDIANT"})
    @RequestMapping(value = "/etudiants")
    public Page<Etudiant> listEtudiant(int page,int size){
        return etudiantReository.findAll(new PageRequest(page,size));
    }
}

spring security的配置是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void globalConfig(AuthenticationManagerBuilder auth) throws Exception{
        // Type d'authentification (Base de données, LDAP, Mémoire)
        auth.inMemoryAuthentication()
                .withUser("admin").password("123456")
                .roles("ADMIN","PROF");
        auth.inMemoryAuthentication()
                .withUser("prof").password("123456")
                .roles("PROF");
        auth.inMemoryAuthentication()
                .withUser("etudiant1").password("123456")
                .roles("ETUDIANT");
        auth.inMemoryAuthentication()
                .withUser("scolaritel").password("123456")
                .roles("SCOLARITE");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Pattern builder
        http
            .csrf().disable()
            .authorizeRequests()
                .anyRequest()
                    .authenticated()
                        .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
            .defaultSuccessUrl("/index.html")
            .failureUrl("/error.html");
    }
}

Mon contrôleur fonctionne bien mais lorsque j'utilise la annotation @Secured et j'authentifier j'avais l'exception :

java.lang.NullPointerException: null 在 com.upsys.sec.service.EtudiantRestService.saveEtudiant(EtudiantRestService.java:29) ~[classes/:na] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_102] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_102] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_102] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_102] 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE] 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE] 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE] 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapt ....

【问题讨论】:

  • EtudiantRestService 的第 29 行是哪一行?
  • @secure 看起来没有问题。检查是否: 1. EtudiantRepository 是否正确自动装配。 2.从请求体中反序列化Etudiant对象的方法。
  • 对不起,我解决了问题

标签: java spring spring-boot spring-security


【解决方案1】:

只是saveEtudiant方法的提名错误,是private 真的很抱歉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多