2017-07-19 12 views
1

私はJava開発で新しく、メソッドレベルで@securedアノテーションを使用してスプリングセキュリティを実装しようとしています。@securedメソッドを使用しているときに "AuthenticationオブジェクトがSecurityContextに見つかりませんでした"

私は私が「/ログイン」私は次のようなエラーになっています呼び出し:

"An Authentication object was not found in the SecurityContext" 

PSを:私は、ログインのための.jspファイルを使用していない、このエラーを管理するためにどのように任意のアイデア?

これは私のコードセクションです。

@Controller 
@RequestMapping("/login") 
public class LoginController { 
@Autowired 
public IUserService userService; 
@RequestMapping(method = RequestMethod.GET) 
public String success(ModelMap map) {  
    userService.addUser("ram", "con1234"); 
    userService.deleteUser("ABC"); 
    map.addAttribute("msg", "Done Successfully"); 
    return "success"; 
    } 
} 

public interface IUserService { 
@Secured ({"ROLE_USER", "ROLE_ADMIN"}) 
public void addUser(String name, String pwd); 
@Secured("ROLE_ADMIN") 
public void deleteUser(String name); 
} 


@Repository 
public class UserServiceSec implements IUserService { 

@Override 
public void addUser(String name, String pwd) { 
    System.out.println("user added"); 
} 
@Override 
public void deleteUser(String name) { 
    System.out.println("user deleted"); 
} 

} 

web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
id="WebApp_ID" version="3.1"> 
<display-name>EQUADIS</display-name> 
<welcome-file-list> 
    <welcome-file>/login</welcome-file> 
</welcome-file-list> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring-config.xml 
     /WEB-INF/security-config.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

セキュリティ-config.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans- 3.2.xsd 
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

<http auto-config="true"> 
    <intercept-url pattern="/login" access="ROLE_USER,ROLE_ADMIN" /> 
    <logout logout-success-url="/login" /> 
</http> 

<authentication-manager> 
    <authentication-provider> 
    <user-service> 
    <user name="ram" password="con1234" authorities="ROLE_ADMIN" /> 
    <user name="rahim" password="con1234" authorities="ROLE_USER" /> 
    </user-service> 
    </authentication-provider> 
</authentication-manager> 

<global-method-security secured-annotations="enabled" /> 
<beans:bean name="userServiceSec" class="package.service.UserServiceSec"/> 

答えて

0

web.xmlにDelegatingFilterProxyフィルタがないようです。あなたは次のものを持っている必要があります。

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
+0

ユーザー名とパスワードページでログインを私は、ユーザー名に入れ何でも、その後示されており、あなたに私は私のweb.xmlにこれらの行を追加して、あなたのreply.afterためshazinと使用して、サーバー上でプロジェクトを実行「/ログインを」感謝パスワードは私に次のようなエラーが表示されます: "元のサーバーがターゲットリソースの現在の表現を見つけられなかったか、または存在することを知らせたくない"という春のセキュリティをチェックしていないように見えますか?私はリンクをテストするときに、ログインページのhtml応答を返します。あなたの助けをもう一度お忘れなく。 – Elias

関連する問題