我做错了什么?我需要Springboot不要求登录在一个特定的url



我有一个应用程序与sprint启动,需要登录,我想要实现的是,如果他们输入一个特定的URL,它不要求他们的用户名或密码。

本页将是一个外部表格,供公众填写。

发生在我身上的是,如果我输入URL,它将我重定向到登录页面,如果我登录了,它将我带到我想要的页面,但如果没有,它将我重定向到登录页面。

如果客户端输入url http://domain/FormClient

我希望应用程序不要求登录。

@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String LOGIN_PROCESSING_URL = "/login";
private static final String LOGIN_FAILURE_URL = "/login?error";
private static final String LOGIN_URL = "/login";
private static final String LOGOUT_SUCCESS_URL = "/login";
@Autowired
private DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled from users where username=?")
.authoritiesByUsernameQuery("select username, role from users where username=?")
;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requestCache().requestCache(new CustomRequestCache())
.and()
.authorizeRequests().antMatchers("/FormClient").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage(LOGIN_URL).permitAll()
.loginProcessingUrl(LOGIN_PROCESSING_URL)
.failureUrl(LOGIN_FAILURE_URL)
.and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL)}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers(
"/VAADIN/**",
"/favicon.ico",
"/robots.txt",
"/sms/",
"/manifest.webmanifest",
"/sw.js",
"/offline.html",
"/icons/**",
"/images/**",
"/styles/**",
"/h2-console/**");
}

侦听器类是这样的

/*如果没有经过认证且URL与Login不同,重定向到Login我已经试着在这行下注释了,但是没有用*/

@Component 
public class ConfigureUIServiceInitListener implements VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addUIInitListener(uiEvent -> { 
final UI ui = uiEvent.getUI();
ui.addBeforeEnterListener(this::authenticateNavigation);
});
}
private void authenticateNavigation(BeforeEnterEvent event) {
if (!LoginView.class.equals(event.getNavigationTarget())
&& !SecurityUtils.isUserLoggedIn()) { 
event.rerouteTo(LoginView.class);
}
}
}

我做错了什么?

你能帮我吗?

非常感谢您的宝贵时间。

在vadin中有一个实现接口的类。

我只是写了这个代码:

@Component 
public class ConfigureUIServiceInitListener  implements VaadinServiceInitListener {
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addUIInitListener(uiEvent -> {
final UI ui = uiEvent.getUI();
ui.addBeforeEnterListener(this::authenticateNavigation);
});
}
private void authenticateNavigation(BeforeEnterEvent event) {
if (!LoginView.class.equals(event.getNavigationTarget())
&& !SecurityUtils.isUserLoggedIn()) {
if(event.getNavigationTarget().getName().endsWith("FormClient"))
{
System.out.println("Retorna porque es FormClient");
return;
}
if(event.getNavigationTarget().getName().endsWith("FormClientCertified"))
{
System.out.println("Retorna porque es FormClientCertified");
return;
}
System.out.println("redirecciona ["+event.getNavigationTarget().getName());
event.rerouteTo(LoginView.class);
}
}
}

现在正在工作,所以当有人请求以FormClient结尾的URL时,它不会重定向到login。

最新更新