gpt4 book ai didi

java -/j_spring_security_check HTTP错误404

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:33 24 4
gpt4 key购买 nike

看起来有些过滤器没有添加。我将 Spring security 3.2.0.RELEASE 与 java-config 一起使用。完整项目发布于 GitHubSecurityConfig.java 在这里:SecurityConfig.java

我尝试在以下位置设置过滤器:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/app/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/")
.defaultSuccessUrl("/app/")
.failureUrl("/?error=1")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/?logout");
}

在 csrf().disable() 之后——但是问题没有解决……请帮助我解决这个问题,因为我可以将/j_spring_security_check 与我自己的 CustomUserDetailsS​​ervice 一起使用!

最佳答案

我没有使用 Spring Security Java Config 的经验,但我检查了您的代码和 API,似乎设置登录处理 URL 会让您登录:

AbstractAuthenticationFilterConfigurer.loginProcessingUrl("/j_spring_security_check")

所以你的代码应该是:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/app/**").hasRole("ADMIN")
.and()
.formLogin()
.loginProcessingUrl("/j_spring_security_check")
.loginPage("/")
.defaultSuccessUrl("/app/")
.failureUrl("/?error=1")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/?logout");
}

我希望这是默认设置。

此外,要使用 MyCustomUserDetailsS​​ervice,而不是像现在这样 Autowiring 它(由 Spring 创建的代理),我会手动配置它:

public class MyCustomUserDetailsService implements UserDetailsService {

private UserDAO userDAO;

public MyCustomUserDetailsService(UserDAO userDAO) {
this.userDAO = userDAO;
}
// ...
}

注意,没有@Service/@Component 注释和通过 Ctor 注入(inject)的 DAO。在安全配置中:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Autowired
private UserDAO userDAO;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.and()
.userDetailsService(new MyCustomUserDetailsService(userDAO));
}
// ...
}

现在我确定 UserDetailService 已正确配置。并且肯定会在登录应用程序时使用它。

我还注意到没有使用用户名和密码。这是因为在 login.jsp 中,您使用 j_username 和 j_password,而用户名参数应该是用户名,密码参数应该是密码。

<input type="text" id="username" class="span4" name="username" placeholder="Username" />
<input type="password" id="password" class="span4" name="password" placeholder="Password" />

查看 FormLoginConfigurer 类。

关于java -/j_spring_security_check HTTP错误404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com