【发布时间】:2016-03-20 02:09:46
【问题描述】:
我有两种不同类型的用户。
- SSO 用户
- 数据库用户。
SSO 用户可能已经通过不同系统的身份验证,而 DB 用户应该通过我们的系统进行身份验证。我可以配置 Spring 安全性来处理这种情况,我可以为某些用户说提示登录页面,而不提示某些用户。
假设对于 SSO 用户,我可以在请求标头中获取用户 ID,而当 DB 访问应用程序时,请求标头中没有用户 ID。我该如何处理这种情况?
我可以通过扩展 DaoAuthenticationProvider 的身份验证方法来覆盖它,然后在某些参数上的页面决定对用户进行身份验证还是有其他方法?我可以向身份验证类添加任何信息吗
这是我尝试过的
安全配置 java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder builder) throws Exception {
builder.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
.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().anyRequest().authenticated().and().httpBasic()
.and().addFilterBefore(new UserTypeFilter(), BasicAuthenticationFilter.class);
}
public PasswordEncoder passwordEncoder() {
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
/*
* @Bean public MethodSecurityInterceptor methodSecurityService() { return
* new MethodSecurityInterceptor(); }
*/
@Bean(name="myAuthenticationManager")
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public ExceptionTranslationFilter exceptionTranslationFilter() {
ExceptionTranslationFilter exceptionTranslationFilter = new ExceptionTranslationFilter(
new Http403ForbiddenEntryPoint());
AccessDeniedHandlerImpl accessDeniedHandlerImpl = new AccessDeniedHandlerImpl();
accessDeniedHandlerImpl.setErrorPage("/exception");
exceptionTranslationFilter
.setAccessDeniedHandler(accessDeniedHandlerImpl);
exceptionTranslationFilter.afterPropertiesSet();
return exceptionTranslationFilter;
}
@Bean
public UserTypeFilter authenticationFilter() throws Exception {
UserTypeFilter authFilter = new UserTypeFilter();
authFilter.setAuthenticationManager(authenticationManager());
return authFilter;
}
}
我的自定义 AbstractAuthenticationProcessingFilter
public class UserTypeFilter extends AbstractAuthenticationProcessingFilter {
private static final String INTERCEPTOR_PROCESS_URL = "/index";
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
public UserTypeFilter() {
super(INTERCEPTOR_PROCESS_URL);
}
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
System.out.println(" BASIC AUTHENTICATION FILTER");
String userId = request.getHeader("USERID");
if (userId == null) {
System.out.println(" THROWING EXCEPTION FILTER");
throw new PreAuthenticatedCredentialsNotFoundException("USERID param not found");
}
return null;
}
}
我的控制器
@Controller
public class MainController {
@RequestMapping(value = { "/index" }, method = RequestMethod.GET)
public ModelAndView index() {
ModelAndView model = new ModelAndView();
model.addObject("message", "This is test page!");
model.setViewName("dummy");
return model;
}
}
控件转到我的自定义过滤器,然后在引发异常但未调用 ExceptionTranslationFilter 时
我是否正确配置了 httpsecurity 我是否正确配置了我的自定义过滤器 我是否配置了 ExceptionTranslation 过滤器 我有什么遗漏吗
【问题讨论】:
-
你有没有想过在 springsecurityfilterchain 中添加过滤器?
-
所以请求头中有一个userId。是否有任何密码,或者只要 userId 引用存在的用户,您是否认为请求有效?如何确定请求标头中提供的 userId 是否为有效的 userId?
-
不,如果他存在与否,我将验证 DB 中的用户 ID。如果用户 ID 存在,那么他将被重定向到主页,否则将重定向到无效用户页面。如果请求标头中不存在用户 ID,则我们将重定向到登录页面
标签: java spring spring-mvc authentication spring-security