如何将spring boot安全api连接到android应用程序

bgibtngc  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(331)

嗨,我用spring boot security做了一个后端api,用android studio做了一个前端应用。当我尝试连接时,它不会将我重定向到主页,它会一直卡在登录页面中,即使我看到在尝试登录时使用正确的登录数据调用userdetails服务我与postman一起测试了重定向是否正确

Java @Configuration class

@Autowired
private UserDetailsService userDetailsService;

@Bean
public AuthenticationProvider authProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(userDetailsService);
    provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance());
    return provider;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
}

@Bean
public PasswordEncoder getPasswordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

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

    http
        .formLogin().loginPage("/").permitAll()
        .defaultSuccessUrl("/home", true)
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers("/static/**","/css/**").permitAll()
        .antMatchers("/home").hasRole("USER")
        .anyRequest().authenticated();

}

    Android Studio:

    final String URL = "http://10.0.2.2:8080/?username=admin&password=pass1/";

    // Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(Login.this);
    StringRequest req = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    Log.d("Response", response.toString());
                    Toast.makeText(Login.this,response,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Log.d("Response", error.toString());
                    Toast.makeText(Login.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            })

    {

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题