【发布时间】:2019-05-29 20:28:05
【问题描述】:
您好,我正在使用带有基本身份验证的 RESTful,此代码是 RestController 的一部分:
@GetMapping("/jpa/users/{username}/goals")
public List<Goal> getAllGoals(@PathVariable String username) {
userId = getUserIdFromUsername(username);
return goalJpaRepository.findByUserId(userId);
}
public Long getUserIdFromUsername(String username) {
User user = userJpaRepository.findByUsername(username);
userId = user.getId();
return userId;
}
我有一个问题,例如我正在使用 Postman 来检索特定用户的目标,如下所示:
http://localhost:8080/jpa/users/john/goals 带有 GET 请求
然后我使用用户名 john 的基本身份验证和此用户名的密码,我收到了 john 的目标。
之后,如果我对此链接 http://localhost:8080/jpa/users/tom/goals 发出 GET 请求,我会收到 tom 的目标,但此时我使用 john 登录,所以 john 可以看到他的目标,也可以看到 tom 的目标目标。
问题是如何访问 RestController 中的登录用户名,因为我想做这样的事情:
if (loginUsername == username) {
return goalJpaRepository.findByUserId(userId);
}
return "Access denied!";
所以我想知道是否可以从 HTTP Header 访问登录用户名?
谢谢!
更新 - 是的,框架是 Spring Boot,我也在使用带有 Dao 身份验证的 Spring Security,因为我想从 MySQL 数据库中获取用户。无论如何,我不是 Spring Security 方面的专家。
现在我了解了如何在我的控制器方法中使用 Principal,但我不知道如何在这种特定情况下使用 Spring Security。我应该如何实施?例如,用户 john 应该只看到和修改他的目标。
Spring 安全配置:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import com.dgs.restful.webservices.goaltrackerservice.user.MyUserDetailsService;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
private MyUserDetailsService userDetailsService;
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider
= new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(bCryptPasswordEncoder());
return authProvider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/allusers").permitAll()
.anyRequest().authenticated()
.and()
// .formLogin().and()
.httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
}
【问题讨论】:
-
你在用弹簧吗?特别是spring-security?
-
旁注,
loginUsername的数据类型是什么?如果它的 String,那么你的比较是错误的。 -
这取决于您使用的框架(如果您正在使用)。您说的是
RestController,所以我猜您正在使用Spring(您绝对应该在您的问题中添加适当的标签)。如果是这样,请查看 Spring 安全性,它允许您配置如何保护您的应用程序。
标签: java rest basic-authentication restful-authentication spring-rest