0

我厌倦了寻找如何通过 Spring Security REST json 登录。我为 Android/iOS 编写后端。这是我的security.xml:

<http use-expressions="true" create-session="stateless" entry-point-ref="restAuthenticationEntryPoint">        
        <intercept-url pattern="/auth/**" access="permitAll" />
        <intercept-url pattern="/**" access="isAuthenticated()" />      
        <custom-filter ref="myFilter" position="FORM_LOGIN_FILTER"/>  
        <logout />               
    </http> 

    <beans:bean id="myFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
          <beans:property name="authenticationManager" ref="authenticationManager"/>
          <beans:property name="authenticationSuccessHandler" ref="mySuccessHandler"/>
    </beans:bean>
    <beans:bean id="mySuccessHandler" class="com.teamodc.jee.webmail.security.MySavedRequestAwareAuthenticationSuccessHandler"/>


    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userDetailsService" />   
        <authentication-provider ref="authenticationProvider" />
    </authentication-manager> 

    <beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService"/>
    </beans:bean> 

这是我的身份验证控制器:

@Controller
@RequestMapping(value = "/auth")
public class AuthorizationController {

    @Autowired
    @Qualifier(value = "authenticationManager")
    AuthenticationManager authenticationManager;

    private SimpleGrantedAuthority anonymousRole = new SimpleGrantedAuthority("ROLE_ANONYMOUS");

    @RequestMapping(value = "/login", method = RequestMethod.POST, headers = {"Accept=application/json"})
    @ResponseBody
    public Map<String, String> login(@RequestParam("login") String username, @RequestParam("password") String password) {
        Map<String, String> response = new HashMap<String, String>();


            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);

            try {
                Authentication auth = authenticationManager.authenticate(token);
                SecurityContextHolder.getContext().setAuthentication(auth);

                response.put("status", "true");             
                return response;
            } catch (BadCredentialsException ex) {
                System.out.println("Login 3");
                response.put("status", "false");
                response.put("error", "Bad credentials");
                return response;
            }
        }

最后,我的web.xml:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/appServlet/servlet-context.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/dispatcher.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<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>

我已经从 Firefox rest 客户端对其进行了测试,但是当我将 URL 设置为 bla/user/1 时,我花了 401(这是正确的),但是当 URL 是 bla/auth/login 时,我花了 404,并返回 WARN [org .springframework.web.servlet.PageNotFound] - 但是当我在@Controller 中标记路径时会怎样

4

1 回答 1

1

您的login()方法似乎映射到/auth/auth/login. 方法级别@RequestMapping注释上给出的路径是相对于类级别注释的路径。

尝试将方法级别注释更改为@RequestMapping(value = "/login"....

编辑:
如果这只是一个错字,并且处理程序映射仍然存在问题,请确保您在 spring 上下文中有适当的说明:

  1. <context:component-scan base-package="package.for.controllers"/>为了让你的控制器实例化为spring bean。
  2. <mvc:annotation-driven/>为了支持带@RequestMapping注释的控制器方法。有关更多信息,请参阅参考文档

此外,请确保它们实际上在相同的上下文中(通常在 servlet 上下文中)。

于 2013-03-26T09:46:56.407 回答