AuthenticationProvider
を実装して、アプリケーションでSpringセキュリティを使用してカスタム認証を実装しようとしています。認証は成功し、ユーザーも指定された役割を持っていますが、私は常にアクセス拒否ページを取得しています。以下は私のコードです。私は春のセキュリティに新しいです。助けてください。事前のおかげでカスタム認証プロバイダでスプリングセキュリティを提供するアクセス拒否エラー
春のsecurity.xml
<form-login
login-page="/login" login-processing-url="/j_spring_security_check" default-target-url="/welcome" authentication-failure-url="/login?error"
/>
<access-denied-handler error-page="/403" />
<logout logout-success-url="/login?logout" />
<csrf disabled="true"/>
</http>
<authentication-manager id="dao-auth" erase-credentials="false">
<authentication-provider ref="customAuthenticationProvider">
</authentication-provider>
</authentication-manager>
<b:bean id="customAuthenticationProvider" class="com.xom.custom.dataservice.impl.CustomAuthenticationProvider"></b:bean>
CustomAuthenticationProvider
@Override
public Authentication authenticate(Authentication authentication) throws
AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
final User rasUser;
try {
rasUser = checkPrivileges(name, password);
} catch (NoRASUserLoginException exception) {
throw new ServiceException(0, "exception while retrieving user data " + exception);
} catch (SQLException exception) {
throw new ServiceException(0, "exception while retrieving user privilages " + name + exception);
}
// userValue = (UserDetails) rasUser;
if (rasUser == null)
throw new UsernameNotFoundException(name + " not found");
List<SimpleGrantedAuthority> auths = new
java.util.ArrayList<SimpleGrantedAuthority>();
for (String privilege : rasUser.getPermissions()) {
if (privilege != null && privilege.equalsIgnoreCase("RReportAdmin"))
{
auths.add(new
SimpleGrantedAuthority("ROLES_".concat(privilege)));
}
}
auths = auths.stream().distinct().collect(Collectors.toList());
authentication = new UsernamePasswordAuthenticationToken(name, password, auths);
return authentication;
}
login.jspを
<html>
<head>
<title>Login</title>
</head>
<body onload='document.loginForm.username.focus();'>
<h1>Spring Security Custom Login Form (XML)</h1>
<div id="login-box">
<h3>Login with Username and Password</h3>
<form name='loginForm'
action="<c:url value='/j_spring_security_check' />" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="submit" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
ログ
2017年11月7日03:47:42212 DEBUG o.s.s.w.u.m.AntPathRequestMatcher [HTTP-NIO-8080-EXEC-15]要求の一致チェック: '/管理'; '/ admin'に対して 2017-11-07 03:47:42,214 DEBUG o.s.s.a.A.AbstractSecurityInterceptor [http-nio-8080-exec-15]セキュアオブジェクト:FilterInvocation:URL:/ admin;属性:[hasRole( 'ROLES_RReportAdmin')] 2017-11-07 03:47:42,214以前に認証されたorg.springframew[email protected]e68aaf8b: プリンシパル:rparwee;資格:[プロテクト];認証済み:真; 詳細: org.sprin[email protected]1c07a: RemoteIpAddress:127.0.0.1; SessionId: EE3501D56ED257409E40A4F8D5F6F794;確かに当局: ROLES_RReportAdmin 2017年11月7日03:47:42216 DEBUG ossavAffirmativeBased [HTTP-NIO-8080-EXEC-15]投票者: org.sp[email protected]6102b9a6、 戻り値:-1 2017-11-07 03:47:42,219 TRACE oscsAbstractApplicationContext [http-nio-8080-exec-15] のWebApplicationContextでのイベントの公開 'mvc-dispatcher-servlet': org.springframework.security .access.event.AuthorizationFailureEvent [source = FilterInvocation: URL:/ admin] 2017-11-07 03:47:42,219 DEBUG osswaExceptionTranslationFilter [http-nio-8080-exec-15]アクセスが拒否されました。匿名); AccessDeniedHandlerへの委託 org.springframework.security.access.AccessDeniedException:アクセスが拒否されました。 org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:84) 〜[spring-security-core-4.2。 3.RELEASE.jar:4.2.3.RELEASE] org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:233) 〜[spring-security-core-4.2.3.RELEASE.jar] :4.2.3.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:124) 〜[spring-security-web-4.2.3.RELEASE.jar:4.2。 3.RELEASE] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(Filter SecurityInterceptor.java:91) 〜[spring-security-web-4.2.3.RELEASE.jar:4.2.3。RELEASE]
応答をありがとうございます。しかし、これを追加した後も動作していません。私はユーザー名とパスワードをハードコーディングしてみました。しかし、DBから詳細を取得している間は動作しません。 –
成功ログ o.s.s.w.u.m.AntPathRequestMatcher [http-nio-8080-exec-25]リクエストの一致を確認しています: '/ admin'; '/ admin **'に対して o.s.s.a.i.AbstractSecurityInterceptor [http-nio-8080-exec-25]セキュアオブジェクト:FilterInvocation:URL:/ admin;属性:[hasRole( 'ROLE_USER')] –
ossaiAbstractSecurityInterceptor [http-nio-8080-exec-25]以前に認証された:org.springframew[email protected]840354ab:プリンシパル:org.springframework.security.core.userdetails .User @ c01cbded:ユーザー名:mkyong;守られたパスワード]; Enabled:true; AccountNonExpired:true; credentialsNonExpired:true; AccountNonLocked:true;付与された権限:ROLE_USER;資格:[プロテクト];認証済み:真;詳細:org.sprin[email protected]2eb76:;許可された権限:ROLE_USER –