Android 需要有关RestTemplate交换方法的帮助吗
我需要使用spring boot从我的应用程序android进行身份验证,我尝试发送用户名和密码,如下所示:Android 需要有关RestTemplate交换方法的帮助吗,android,spring-boot,resttemplate,Android,Spring Boot,Resttemplate,我需要使用spring boot从我的应用程序android进行身份验证,我尝试发送用户名和密码,如下所示: HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader);
HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
并使用restTemplate.exchange发出网络请求:
ResponseEntity<Message> response = restTemplate.exchange(url, HttpMethod.POST,new HttpEntity<Object>(requestHeaders), Message.class);
配置
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout()
.permitAll();
http.exceptionHandling().accessDeniedPage("/403");
}
}
我相信
RController
类需要用@RestController
注释,而不是@Controller
尝试将和()
method谢谢,我会一直尝试,直到不起作用:/…任何建议,先生,请你是对的…我做了更改,但仍然不起作用…从我的搜索中,我发现帖子需要参数才能发送到请求网络,我想知道我该怎么做!我建议首先尝试让端点在没有安全性的情况下工作,然后添加安全性@RestController
只是@Controller
和@ResponseBody'注释的语法糖。因为
@ResponseBody`注释已经在getMessage()
方法的上方,所以您的建议不会影响任何东西,除非中断login()
方法,该方法将返回字符串值'login',而不是视图名为login的值。@ootero oki谢谢您…如果您有教程或文档,请提供帮助
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout()
.permitAll();
http.exceptionHandling().accessDeniedPage("/403");
}
}