@RequiresRoles注释在shiro中不起作用

时间:2017-11-29 08:22:27

标签: java annotations jersey shiro

我正在尝试探索shiro框架。身份验证工作正常。授权时我遇到了一个问题。我正在使用shiro注释@RequiresRoles进​​行授权。虽然我正在使用注释,但它不仅仅授权我指定的角色。它允许所有角色。请找到我尝试过的下面的代码。我正在使用球衣和shiro。

Java代码: -

   class Drukarka
    {
        public TextBox TextBox_Drukarki_IPNS { get; set; }
        public TextBox TextBox_Drukarki_IP { get; set; }
        public TextBox TextBox_Drukarki_NS { get; set; }
        public TextBox TextBox_Drukarki_Salon { get; set; }
        public TextBox TextBox_Drukarki_Terminal { get; set; }
        public TextBox TextBox_Drukarki_Status { get; set; }
        public TextBox TextBox_Drukarki_Serwer { get; set; }
        public TextBox TextBox_Drukarki_Model { get; set; }
        public TextBox TextBox_Drukarki_Podlaczenie { get; set; }
        public TextBox TextBox_Drukarki_Lokalizacja { get; set; }

        public async Task CheckCSV(TextBox TextBox_Drukarki_IPNS, TextBox TextBox_Drukarki_IP, TextBox TextBox_Drukarki_NS, TextBox TextBox_Drukarki_Salon, TextBox TextBox_Drukarki_Terminal, TextBox TextBox_Drukarki_Status,
            TextBox TextBox_Drukarki_Serwer, TextBox TextBox_Drukarki_Model, TextBox TextBox_Drukarki_Podlaczenie, TextBox TextBox_Drukarki_Lokalizacja)
        {
            try
            {
                if ((TextBox_Drukarki_IPNS.Text == "" || TextBox_Drukarki_IPNS.Text == " "))
                {
                    var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
                    if (window != null)
                        await window.ShowMessageAsync("Błąd!", "Podaj numer seryjny!");
                    return;
                }
                else
                {
                    TextBox_Drukarki_IPNS.Text = string.Join("", TextBox_Drukarki_IPNS.Text.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));

                    Color Kolor = Color.FromRgb(37, 37, 37);
                    Color Kolor2 = Color.FromRgb(255, 160, 0);

                    TextBox_Drukarki_Terminal.Background = new SolidColorBrush(Kolor);
                    TextBox_Drukarki_Lokalizacja.Background = new SolidColorBrush(Kolor);
                    TextBox_Drukarki_Serwer.Background = new SolidColorBrush(Kolor);
                    TextBox_Drukarki_Salon.Background = new SolidColorBrush(Kolor);
                    TextBox_Drukarki_Podlaczenie.Background = new SolidColorBrush(Kolor);
                    TextBox_Drukarki_Status.Background = new SolidColorBrush(Kolor);

                    //TextFieldParser sprawdz = new TextFieldParser(@"C:\Users\Przemysław\OneDrive\Programowanie\wykaz_druk.csv");
                    TextFieldParser sprawdz = new TextFieldParser(@"\\DSB192\Aplikacja_PrintFix$\Raporty\wykaz_druk.csv");
                    string currentLine;
                    sprawdz.TextFieldType = FieldType.Delimited;
                    sprawdz.Delimiters = new string[] { ";" };
                    sprawdz.TrimWhiteSpace = true;
                    bool czyjest = false;

                    do
                    {
                        currentLine = sprawdz.ReadLine();
                        if (currentLine != null)
                        {
                            string file = currentLine;

                            string STATUS = file.Split(';')[0].Trim();
                            string MODEL = file.Split(';')[1].Trim();
                            string NS = file.Split(';')[2].Trim();
                            string IPD = file.Split(';')[3].Trim();
                            string SERWER = file.Split(';')[4].Trim();
                            //string ??? = file.Split(';')[5].Trim(); kolejka
                            string UDZIAL = file.Split(';')[6].Trim();
                            string LOKAL = file.Split(';')[7].Trim();
                            string SALON = file.Split(';')[8].Trim();
                            //string ??? = file.Split(';')[9].Trim(); lastcom
                            string PODLACZ = file.Split(';')[10].Trim();

                            if (IPD == TextBox_Drukarki_IPNS.Text || NS == TextBox_Drukarki_IPNS.Text)
                            {
                                czyjest = true;
\\ALOT CODE WITH FORWARDING TO TEXT.BOXES and few await window.ShowMessageAsync

                            }
                        }
                    } while (currentLine != null);

                    if (czyjest == false)
                    {
                        var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
                        if (window != null)
                            await window.ShowMessageAsync("Błąd!", "Błędny numer seryjny!");
                        return;
                    }
                }
            }
            catch (Exception)
            {
                var window = Application.Current.Windows.OfType<MetroWindow>().FirstOrDefault();
                if (window != null)
                    await window.ShowMessageAsync("Błąd!", "Błąd pobierania danych.");
                return;
            }
        }
    }

}

Web.xml中

package com.somecompany.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;

@Path("/test")
public class HelloWorldService {

      @GET
      @Path("/{param}")
      @RequiresRoles(value= {"admin_role"})
      public Response getMsg(@PathParam("param") String msg) {

            String output = "Jersey say : " + msg;
            System.out.println(SecurityUtils.getSubject().hasRole("admin_role"));

        return Response.status(200).entity(output).build();

  }

的pom.xml

   <servlet>
            <servlet-name>jersey-serlvet</servlet-name>
            <servlet-class>
                         com.sun.jersey.spi.container.servlet.ServletContainer
                    </servlet-class>
            <init-param>
                 <param-name>com.sun.jersey.config.property.packages</param-name>
                 <param-value>com.somecompany.rest</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>jersey-serlvet</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>


        <listener>
            <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
        </listener>

        <filter>
            <filter-name>ShiroFilter</filter-name>
            <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
        </filter>

        <filter-mapping>
            <filter-name>ShiroFilter</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>INCLUDE</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>

shiro.ini

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.17</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.17</version>
</dependency>

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- Included commons logging for shiro -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-aspectj</artifactId>
    <version>1.3.2</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

看起来您的pom.xml缺少以下Shiro JAX-RS支持库:

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-jaxrs</artifactId>
    <version>1.4.0</version>
</dependency>

将此依赖项添加到项目后 - @RequiresRoles必须正常工作。