gpt4 book ai didi

java - Spring Security 匿名用户可以访问每个 url

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

我正在开发 gwt 应用程序,我想使用 spring-security 来保护它。我在数据库中有用户数据,UserService 负责获取特定用户。我已经关注了这个tutorial

身份验证提供者:

public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired UserService userService;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = (String) authentication.getPrincipal();
String password = (String) authentication.getCredentials();

User user = userService.findByUserName(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
String storedPass = user.getPassword();
if (!storedPass.equals(password)) {
throw new BadCredentialsException("Invalid password");
}
Authentication customAuthentication = new CustomUserAuthentication(user, authentication);
customAuthentication.setAuthenticated(true);

return customAuthentication;
}

@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}

自定义身份验证

    public class CustomUserAuthentication implements Authentication {

private static final long serialVersionUID = -3091441742758356129L;

private boolean authenticated;

private final GrantedAuthority grantedAuthority;
private final Authentication authentication;
private final User user;

public CustomUserAuthentication(User user, Authentication authentication) {
this.grantedAuthority = new SimpleGrantedAuthority(user.getRole().name());
this.authentication = authentication;
this.user = user;
}

@Override
public Collection<GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(grantedAuthority);
return authorities;
}

@Override
public Object getCredentials() {
return authentication.getCredentials();
}

@Override
public Object getDetails() {
return authentication.getDetails();
}

@Override
public Object getPrincipal() {
return user;
}

@Override
public boolean isAuthenticated() {
return authenticated;
}

@Override
public void setAuthenticated(boolean authenticated) throws IllegalArgumentException {
this.authenticated = authenticated;
}

@Override
public String getName() {
return user.getUsername();
}

}

安全上下文:

<s:http auto-config="true" create-session="always" >
<s:intercept-url pattern="/index.html" access="ROLE_USER" />
<s:logout logout-success-url="/login.html"/>
<s:form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login.html" />
</s:http>

<s:authentication-manager alias="authenticationManager">
<s:authentication-provider ref="customAuthenticationProvider" />
</s:authentication-manager>

<bean id="customAuthenticationProvider" class="com.example.server.security.CustomAuthenticationProvider" />

一切正常,spring 拦截调用我需要记录的 index.html 并将我重定向回 index.html。问题是当我注销然后再次转到 index.html 时,我只是简单地访问它。我想通了:

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("Logged as: " + auth.getName());

注销后打印 anonymousUser。当我再次登录时,此代码会打印我的用户名,因此我认为拦截匿名用户有问题。有谁知道如何拦截匿名用户?

最佳答案

代替:

 <s:intercept-url pattern="/**" access="ROLE_USER" />

您可以使用:

<s:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY,ROLE_USER" />

这应该使 Spring Security 拒绝匿名用户的访问。当然,这意味着您还需要添加其中之一:

<s:intercept-url pattern="/url_that_should_be_accessible_to_anonymous_user" access="IS_AUTHENTICATED_ANONYMOUSLY" />

对于匿名用户应该能够访问的每个模式。通常,登录页面、错误页面、静态资源(图像、PDF 等)。

关于java - Spring Security 匿名用户可以访问每个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379521/

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